在Python中的Pandas DataFrame的时间戳列中查找DateTime
我有一个时间戳记,例如1611903555682,我将其转换为日期时间,即2021-01-29 07:59:15,我想找出该日期时间是否存储在Pandas DataFrame的时间戳列中,看起来像是像:
['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.64400000000''2021-02-08T11:36:10.64900000000']
我希望比较只能查看yyyy-mm-dd hh-mm-ss,因此忽略了毫秒。
我尝试了以下操作,
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
但是即使值实际上在数据框中,也从未输入此条件(我通过打印dataframe对其进行了检查)。我在做转换错误吗?实际上,如果我跑步:
print(str(pd.Timestamp(datetime.fromtimestamp(last_timestamp/1000).strftime("%Y-%m-%d, %H:%M:%S") )))
print(df_processed_timestamps['time'].values)
我得到:
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']
有建议吗?谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的问题是您将
Timestamp
转换为%y-%m-%d,%h:%m:%s
,但time
中的值列不是那种格式。您可以将
time
列转换为秒,然后检查timestamp
是否在列中Your problem is that you convert
timestamp
to%Y-%m-%d, %H:%M:%S
but values intime
column is not that format.You can convert the
time
column to seconds then check iftimestamp
is in column