我有一个带有时间戳列和数字列的数据框.如果timestamp列是时区的,那么我可以向它添加一个新行.
df = pd.DataFrame([[1,2],[3,4]], columns=['timestamp', 'number']) df['timestamp']=pd.to_datetime(df['timestamp']) df # timestamp number # 0 1970-01-01 00:00:00.000000001 2 # 1 1970-01-01 00:00:00.000000003 4 df.append(df.loc[0]) # timestamp number # 0 1970-01-01 00:00:00.000000001 2 # 1 1970-01-01 00:00:00.000000003 4 # 0 1970-01-01 00:00:00.000000001 2
但是,如果我为timestamp列设置时区,然后尝试追加新行,我会收到错误.
df['timestamp']=df['timestamp'].apply(lambda x: x.tz_localize('utc')) df # timestamp number # 0 1970-01-01 00:00:00.000000001+00:00 2 # 1 1970-01-01 00:00:00.000000003+00:00 4 df.append(df.loc[0]) # Traceback (most recent call last): # File "", line 1, in # File "/Library/Python/2.7/site-packages/pandas-0.17.1-py2.7-macosx-10.10-intel.egg/pandas/core/frame.py", line 4231, in append # verify_integrity=verify_integrity) # File "/Library/Python/2.7/site-packages/pandas-0.17.1-py2.7-macosx-10.10-intel.egg/pandas/tools/merge.py", line 813, in concat # return op.get_result() # File "/Library/Python/2.7/site-packages/pandas-0.17.1-py2.7-macosx-10.10-intel.egg/pandas/tools/merge.py", line 995, in get_result # mgrs_indexers, self.new_axes, concat_axis=self.axis, copy=self.copy) # File "/Library/Python/2.7/site-packages/pandas-0.17.1-py2.7-macosx-10.10-intel.egg/pandas/core/internals.py", line 4456, in concatenate_block_managers # for placement, join_units in concat_plan] # File "/Library/Python/2.7/site-packages/pandas-0.17.1-py2.7-macosx-10.10-intel.egg/pandas/core/internals.py", line 4561, in concatenate_join_units # concat_values = com._concat_compat(to_concat, axis=concat_axis) # File "/Library/Python/2.7/site-packages/pandas-0.17.1-py2.7-macosx-10.10-intel.egg/pandas/core/common.py", line 2548, in _concat_compat # return _concat_compat(to_concat, axis=axis) # File "/Library/Python/2.7/site-packages/pandas-0.17.1-py2.7-macosx-10.10-intel.egg/pandas/tseries/common.py", line 256, in _concat_compat # return DatetimeIndex(np.concatenate([ x.tz_localize(None).asi8 for x in to_concat ]), tz=list(tzs)[0]) # AttributeError: 'numpy.ndarray' object has no attribute 'tz_localize'
有关如何将新行附加到具有时区感知时间变量列的数据框的任何帮助将非常感激.