I have a timestamp, say 1611903555682, which I convert into a datetime, i.e. 2021-01-29 07:59:15, and I want to find out whether this datetime is stored in a timestamp column of a pandas dataframe, which looks like:
['2021-05-10T19:53:38.205000000' '2021-05-10T19:53:39.210000000' '2021-05-10T19:53:40.215000000' ... '2021-02-08T11:36:08.639000000' '2021-02-08T11:36:09.644000000' '2021-02-08T11:36:10.649000000']
I want the comparison to look only at YYYY-MM-DD HH-MM-SS, so ignoring the milliseconds.
I tried with the following
if pd.Timestamp(datetime.fromtimestamp(last_timestamp/1000).strftime("%Y-%m-%d, %H:%M:%S") ) in df_processed_timestamps['time'].values:
print('Value is already in dataframe.')
return
But this condition is never entered, even if values are actually in the dataframe (I checked it by printing the dataframe). Am I doing a conversion error? In fact, if I run:
print(str(pd.Timestamp(datetime.fromtimestamp(last_timestamp/1000).strftime("%Y-%m-%d, %H:%M:%S") )))
print(df_processed_timestamps['time'].values)
I get:
2021-01-29 07:59:15
['2021-05-10T19:53:38.205000000' '2021-05-10T19:53:39.210000000'
'2021-05-10T19:53:40.215000000' ... '2021-02-08T11:36:08.639000000'
'2021-02-08T11:36:09.644000000' '2021-02-08T11:36:10.649000000']
Any suggestion? Thanks!
CodePudding user response:
Your problem is that you convert timestamp
to %Y-%m-%d, %H:%M:%S
but values in time
column is not that format.
You can convert the time
column to seconds then check if timestamp
is in column
timestamp = 1611903555682
isin = timestamp in pd.to_datetime(df['time']).values.astype(np.int64) // (10**6)
print(pd.to_datetime(df['time']).values.astype(np.int64) // (10**6))
[1620676418205 1620676419210 1620676420215 1612784168639 1612784169644
1612784170649]
print(isin)
False