gpt4 book ai didi

python - pandas 过滤不包含任何内容的日期时间列

转载 作者:行者123 更新时间:2023-11-30 22:47:43 25 4
gpt4 key购买 nike

我有一个 pandas 数据框,其中有两个“日期时间”列 t1,t2。现在我需要过滤掉数据框中 t1<=t2 的所有行t2 可能是 Nan

Pandas 0.19.0之前我可以这样做:

import pandas as pd
from datetime import datetime
dt = datetime.utcnow()
dt64 = np.datetime64(dt)
df = pd.DataFrame([(dt64,None)], columns=['t1','t2'])
df[(df.t1<=df.t2)]

在 pandas 0.19.0 之后此代码失败

Traceback (most recent call last):
File "workspace/python/MyTests/test1.py", line 87, in <module>
testDfTimeCompare()
File "workspace/python/MyTests/test1.py", line 80, in testDfTimeCompare
df[(df.t1<=df.t2)]
File "anaconda/lib/python2.7/site-packages/pandas/core/ops.py", line 813, in wrapper
return self._constructor(na_op(self.values, other.values),
File "anaconda/lib/python2.7/site-packages/pandas/core/ops.py", line 787, in na_op
y = y.view('i8')
File "anaconda/lib/python2.7/site-packages/numpy/core/_internal.py", line 367, in _view_is_safe
raise TypeError("Cannot change data-type for object array.")
TypeError: Cannot change data-type for object array.

实现这一目标的最佳方法是什么。

最佳答案

我认为你需要转换列 t2 to_datetime 对于 Actor NoneNaT ,然后可以使用更快的函数 Series.le <= 相同的是什么:

df.t2 = pd.to_datetime(df.t2)
print (df)
t1 t2
0 2016-11-04 07:24:53.372838 NaT

mask = df.t1.le(df.t2)
print (mask)
0 False
dtype: bool

mask = df.t1 <= df.t2
print (mask)
0 False
dtype: bool

关于python - pandas 过滤不包含任何内容的日期时间列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40417013/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com