我需要在时间戳列上执行合并操作,但行为取决于时区是否设置。
以下代码运行正常
import pandas as pd, datetime
now = datetime.datetime.now()
df1 = pd.DataFrame({'ts': pd.to_datetime([now])})
df2 = pd.DataFrame({'ts': pd.to_datetime([now])})
pd.merge(df1, df2, on='ts')
相反,这并不会。
import pandas as pd, datetime
now = datetime.datetime.now().replace(tzinfo=pytz.utc)
df3 = pd.DataFrame({'ts': pd.to_datetime([now])})
df4 = pd.DataFrame({'ts': pd.to_datetime([now])})
pd.merge(df3, df4, on='ts')
我遇到了以下错误。
/path/to/env3.4/lib/python3.4/site-packages/pandas/tools/merge.py in merge(left, right, how, on, left_on, right_on, left_index, right_index, sort, suffixes, copy, indicator)
33 right_index=right_index, sort=sort, suffixes=suffixes,
34 copy=copy, indicator=indicator)
---> 35 return op.get_result()
36 if __debug__:
37 merge.__doc__ = _merge_doc % '\nleft : DataFrame'
/path/to/env3.4/lib/python3.4/site-packages/pandas/tools/merge.py in get_result(self)
194 self.left, self.right = self._indicator_pre_merge(self.left, self.right)
195
--> 196 join_index, left_indexer, right_indexer = self._get_join_info()
197
198 ldata, rdata = self.left._data, self.right._data
/path/to/env3.4/lib/python3.4/site-packages/pandas/tools/merge.py in _get_join_info(self)
323 right_indexer) = _get_join_indexers(self.left_join_keys,
324 self.right_join_keys,
--> 325 sort=self.sort, how=self.how)
326
327 if self.right_index:
/path/to/env3.4/lib/python3.4/site-packages/pandas/tools/merge.py in _get_join_indexers(left_keys, right_keys, sort, how)
514
515 # get left & right join labels and num. of levels at each location
--> 516 llab, rlab, shape = map(list, zip( * map(fkeys, left_keys, right_keys)))
517
518 # get flat i8 keys from label lists
TypeError: type object argument after * must be a sequence, not map
我的环境:
- python 3.4
- pandas 0.17.0
- numpy 1.10.1
数据类型不同:
In [4]: df1.dtypes
Out[4]:
ts datetime64[ns]
dtype: object
In [5]: df3.dtypes
Out[5]:
ts datetime64[ns, UTC]
dtype: object
pandas中日期的处理有点神秘。您需要知道:
- 如果您操作datetime.datetime、numpy.datetime64或pandas.Timestamp
- 是否带有时区
- 秒/毫秒/微秒/纳秒精度
我漏掉了什么?