在Python中的Pandas DataFrame的时间戳列中查找DateTime

发布于 2025-02-10 08:09:58 字数 1127 浏览 1 评论 0原文

我有一个时间戳记,例如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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅黛梨妆こ 2025-02-17 08:09:58

您的问题是您将Timestamp转换为%y-%m-%d,%h:%m:%s,但time中的值列不是那种格式。

您可以将time列转换为秒,然后检查timestamp是否在列中

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文