我有一个带有时间戳列和数字列的数据框。如果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 "<stdin>", line 1, in <module>
#   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'

我将对如何将新行追加到具有时区感知timesamp列的数据帧提供任何帮助,将不胜感激。

最佳答案

这是该 Pandas 版本中的一个错误(贷记给this answer)。
如他们所说,您的解决方案可以是:

df = df.astype(str).append(df.loc[0].astype(str))
df['timestamp'] = pd.to_datetime(df['timestamp'], utc=True)

关于python - 如何在带有时区感知时间戳列的数据帧上追加数据?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34433088/

10-12 16:20
查看更多