2016-11-11 20 views
0

我有一個熊貓數據幀:大熊貓過濾器日期時間:類型錯誤:無法比擬的偏移天真和偏移感知日期時間

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 

我想在我的熊貓數據幀過濾掉一些行象下面這樣:

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 

這似乎是時區的問題。這裏忽略時區的最好方法是什麼?謝謝!

+0

您可以爲您的數據幀的樣本? (以及它是如何構建的) –

+0

my_df [my_df.my_timestamp> pd.to_datetime(「2016-07-01」)] – piRSquared

+0

@ DennisGolomazov:添加了示例數據框。謝謝! – Edamame

回答

1

假設在數據幀的所有時間戳都在同一時區:

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

運作良好。謝謝! – Edamame

+0

@Edamame很高興它有幫助! –