OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

python pandas dataframe select rows with ternary operator

  • Thread starter Thread starter KBD
  • Start date Start date
K

KBD

Guest
I have what has to be considered a syntax question. Doing some data scrubbing, data contains date time fields. Date is always present but sometimes the time is missing. Like this:

Code:
    3/1/2023 11:03 AM
    4/8/2022 10:21 AM
    7/19/2012
   7/12/2021
   4/16/2024 7:02 AM

So my simple mind says, if the full colon (:) is missing concat 00:00. And we are done.

I expected this to work:

Code:
dfAssetMeter_a['lastReadingDTFixed']  =  dfAssetMeter_a['LASTREADINGDATE'].astype(str)          if dfAssetMeter_a['LASTREADINGDATE'].astype(str).find(':')  > 0 else   dfAssetMeter_a['LASTREADINGDATE'].astype(str)  +  '00:00 AM'

Did not work

This works:

Code:
def fixDateTime(dateStr_in):
    return     dateStr_in  if dateStr_in.find(':')  > 0 else  dateStr_in +  ' 12:00 AM'


dfAssetMeter_a['lastReadingDTFixed']  =  dfAssetMeter_a['LASTREADINGDATE'].apply( fixDateTime  )

Questions:

Which is more correct “Python” way to do this?
In my mind, method two would be easier to understand at three on a Friday , before a holiday weekend. Please I do not want to start a battle royal here!

Would somebody be kind enough to provide the correct syntax for the first method.
<p>I have what has to be considered a syntax question.
Doing some data scrubbing, data contains date time fields.
Date is always present but sometimes the time is missing.
Like this:</p>
<pre><code> 3/1/2023 11:03 AM
4/8/2022 10:21 AM
7/19/2012
7/12/2021
4/16/2024 7:02 AM
</code></pre>
<p>So my simple mind says, if the full colon (:) is missing concat 00:00.
And we are done.</p>
<p>I expected this to work:</p>
<pre><code>dfAssetMeter_a['lastReadingDTFixed'] = dfAssetMeter_a['LASTREADINGDATE'].astype(str) if dfAssetMeter_a['LASTREADINGDATE'].astype(str).find(':') > 0 else dfAssetMeter_a['LASTREADINGDATE'].astype(str) + '00:00 AM'
</code></pre>
<p>Did not work</p>
<p>This works:</p>
<pre><code>def fixDateTime(dateStr_in):
return dateStr_in if dateStr_in.find(':') > 0 else dateStr_in + ' 12:00 AM'


dfAssetMeter_a['lastReadingDTFixed'] = dfAssetMeter_a['LASTREADINGDATE'].apply( fixDateTime )

</code></pre>
<p>Questions:</p>
<p>Which is more correct “Python” way to do this?<br />
In my mind, method two would be easier to understand at three on a Friday , before a holiday weekend.
Please I do not want to start a battle royal here!</p>
<p>Would somebody be kind enough to provide the correct syntax for the first method.</p>
 

Latest posts

S
Replies
0
Views
1
Sergey Bakaev Rettley
S
Top