Python 熊猫过滤器日期时间:TypeError:can';t比较初始偏移量和感知偏移量的日期时间

Python 熊猫过滤器日期时间:TypeError:can';t比较初始偏移量和感知偏移量的日期时间,python,python-2.7,datetime,pandas,filter,Python,Python 2.7,Datetime,Pandas,Filter,我有一个数据框: name my_timestamp ------------------------------------------ 0 a1 2016-07-28 09:27:07.536963-07:00 1 a2 2016-07-28 09:27:07.536963-07:00 2 a3 2016-08-15 13:05:54.924185-07:00 3 a4 2016-08-30 04:04:18.971667

我有一个数据框:

    name    my_timestamp
------------------------------------------
0   a1      2016-07-28 09:27:07.536963-07:00
1   a2      2016-07-28 09:27:07.536963-07:00
2   a3      2016-08-15 13:05:54.924185-07:00
3   a4      2016-08-30 04:04:18.971667-07:00
4   a5      2016-03-22 14:36:22.999825-07:00
5   a6      2016-08-30 04:04:18.971667-07:00
我正在尝试筛选pandas数据框中的一些行,如下所示:

import datetime
my_df[my_df.my_timestamp > datetime.datetime(2016, 7, 1)]
但会出现以下错误:

TypeErrorTraceback (most recent call last)
<ipython-input-21-35be746f191d> in <module>()
      1 import datetime
----> 2 my_df[my_df.my_timestamp > datetime.datetime(2016, 7, 1)]

/usr/local/lib/python2.7/dist-packages/pandas/core/ops.pyc in wrapper(self, other, axis)
    761                 other = np.asarray(other)
    762 
--> 763             res = na_op(values, other)
    764             if isscalar(res):
    765                 raise TypeError('Could not compare %s type with Series' %

/usr/local/lib/python2.7/dist-packages/pandas/core/ops.pyc in na_op(x, y)
    681                     result = lib.vec_compare(x, y, op)
    682             else:
--> 683                 result = lib.scalar_compare(x, y, op)
    684         else:
    685 

pandas/lib.pyx in pandas.lib.scalar_compare (pandas/lib.c:14261)()

TypeError: can't compare offset-naive and offset-aware date times
TypeErrorTraceback(最近一次调用上次)
在()
1导入日期时间
---->2 my_df[my_df.my_timestamp>datetime.datetime(2016,7,1)]
/包装器中的usr/local/lib/python2.7/dist-packages/pandas/core/ops.pyc(self、other、axis)
761其他=np.asarray(其他)
762
-->763 res=na_op(值,其他)
764如果是isscalar(res):
765 raise TypeError('无法将%s类型与序列进行比较'%
/na_op(x,y)中的usr/local/lib/python2.7/dist-packages/pandas/core/ops.pyc
681结果=库向量比较(x,y,op)
682其他:
-->683结果=库标量比较(x,y,op)
684其他:
685
pandas.lib.scalar_比较中的pandas/lib.pyx(pandas/lib.c:14261)()
TypeError:无法比较初始偏移量和感知偏移量的日期时间

这似乎是时区问题。在这里忽略时区的最佳方式是什么?谢谢!

假设数据帧中的所有时间戳都在同一时区:

tz_info = my_df.iloc[0].my_timestamp.tzinfo
my_df[my_df.my_timestamp > datetime.datetime(2016, 7, 1, tzinfo=tz_info)]

你能提供你的数据帧样本吗?(以及它是如何构造的)my_df[my_df.my_timestamp>pd.to_datetime(“2016-07-01”)@DennisGolomazov:添加了样本数据帧。谢谢!@piRSquared:我尝试了你的建议,但仍然是相同的错误。有什么想法吗?谢谢!效果很好。谢谢!@Edamame很高兴能帮上忙!