首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >为什么pandas DataFrame.append()会给出一个时区值错误?

为什么pandas DataFrame.append()会给出一个时区值错误?
EN

Stack Overflow用户
提问于 2018-06-05 05:32:24
回答 1查看 347关注 0票数 1

我有一个在循环中附加的数据框架(如果有更好的方法迭代地将行添加到数据框架的末尾,那么欢迎建议)。下面的代码片段显示了一个错误:

代码语言:javascript
复制
import pandas as pd
import pytz
import datetime

x = 'astring'
t = (datetime.datetime(2018, 5, 31, 13, 15, 17, tzinfo=pytz.utc), datetime.datetime(2100, 5, 31, tzinfo=pytz.utc))
df = pd.DataFrame(columns=['a', 'b', 'c'])
df = df.append({'a': x, 'b': t[0], 'c': t[1]}, ignore_index=True)

TypeError                                 Traceback (most recent call last)
<ipython-input-161-0df455a78607> in <module>()
      2 t = (datetime.datetime(2018, 5, 31, 13, 15, 17, tzinfo=pytz.utc), datetime.datetime(2100, 5, 31, tzinfo=pytz.utc))
      3 df = pd.DataFrame(columns=['a', 'b', 'c'])
----> 4 df = df.append({'a': x, 'b': t[0], 'c': t[1]}, ignore_index=True)

/usr/local/envs/py3env/lib/python3.5/site-packages/pandas/core/frame.py in append(self, other, ignore_index, verify_integrity)
   5192 
   5193     _shared_docs['pivot_table'] = """
-> 5194         Create a spreadsheet-style pivot table as a DataFrame. The levels in
   5195         the pivot table will be stored in MultiIndex objects (hierarchical
   5196         indexes) on the index and columns of the result DataFrame

/usr/local/envs/py3env/lib/python3.5/site-packages/pandas/core/reshape/concat.py in concat(objs, axis, join, join_axes, ignore_index, keys, levels, names, verify_integrity, copy)
    211     a  1
    212     >>> df6 = pd.DataFrame([2], index=['a'])
--> 213     >>> df6
    214        0
    215     a  2

/usr/local/envs/py3env/lib/python3.5/site-packages/pandas/core/reshape/concat.py in get_result(self)
    406             mgrs_indexers = []
    407             for obj in self.objs:
--> 408                 mgr = obj._data
    409                 indexers = {}
    410                 for ax, new_labels in enumerate(self.new_axes):

/usr/local/envs/py3env/lib/python3.5/site-packages/pandas/core/internals.py in concatenate_block_managers(mgrs_indexers, axes, concat_axis, copy)
   5201     expanded label indexer
   5202     """
-> 5203     mult = np.array(shape)[::-1].cumprod()[::-1]
   5204     return _ensure_platform_int(
   5205         np.sum(np.array(labels).T * np.append(mult, [1]), axis=1).T)

/usr/local/envs/py3env/lib/python3.5/site-packages/pandas/core/internals.py in concatenate_join_units(join_units, concat_axis, copy)
   5330 
   5331     # see if we are only masking values that if putted
-> 5332     # will work in the current dtype
   5333     try:
   5334         nn = n[m]

/usr/local/envs/py3env/lib/python3.5/site-packages/pandas/core/internals.py in <listcomp>(.0)
   5330 
   5331     # see if we are only masking values that if putted
-> 5332     # will work in the current dtype
   5333     try:
   5334         nn = n[m]

/usr/local/envs/py3env/lib/python3.5/site-packages/pandas/core/internals.py in get_reindexed_values(self, empty_dtype, upcasted_na)
   5601     for ax, indexer in indexers.items():
   5602         mgr_shape[ax] = len(indexer)
-> 5603     mgr_shape = tuple(mgr_shape)
   5604 
   5605     if 0 in indexers:

TypeError: data type not understood

但是,以下代码段工作正常:

代码语言:javascript
复制
x = 'astring'
t = (datetime.datetime(2018, 5, 31, 13, 15, 17), datetime.datetime(2100, 5, 31))
df = pd.DataFrame(columns=['a', 'b', 'c'])
df = df.append({'a': x, 'b': t[0], 'c': t[1]}, ignore_index=True)

更奇怪的是,这也没问题:

代码语言:javascript
复制
t = (datetime.datetime(2018, 5, 31, 13, 15, 17, tzinfo=pytz.utc), datetime.datetime(2100, 5, 31, tzinfo=pytz.utc))
df = pd.DataFrame(columns=['b', 'c'])
df = df.append({'b': t[0], 'c': t[1]}, ignore_index=True)

我遗漏了什么?我只是在这里添加更多细节,因为StackOverflow抱怨我“需要更多细节”来提交这个问题,因为我想异常冗长是一件好事。谁知道呢?

代码语言:javascript
复制
pandas==0.23.0
pytz==2016.7
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-05 19:29:20

这看起来像是pandaspytz库版本之间的兼容性问题。

我能够重现您在Datalab中获得的错误,并且我能够通过升级到pandas==0.23.0 (我使用的是全新Datalab实例附带的默认0.22.0 )和pytz==2018.4来解决它。此外,根据我看到的其他一些Stack Overflow帖子,numpy可能存在一些问题,所以为了仔细检查,我使用了numpy==1.14.3

为了升级库版本,您应该:

  1. 创建一个新的notebook,并在第一个单元格中运行命令!pip install --upgrade pandas。这为我安装了pytz==2018.4,但如果在您的情况下没有,您也可以尝试手动安装它。
  2. 通过单击Datalab中的"Reset session“选项重新启动内核。
  3. 再次运行您的代码,看看它现在是否可以工作:

添加以下行以检查我提到的版本是否正在使用中:

代码语言:javascript
复制
print(pd.__version__)
print(pytz.__version__)
print(np.__version__)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50689076

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档