" AttributeError:' DataFrame'对象没有属性' dtype'"使用pd.to_datetime

时间:2017-09-15 02:57:16

标签: python string pandas datetime

我一直在Windows机器上使用 Python 2.7.13 来编写我的代码。但我现在正尝试使用Python 2.7.12-goolf-2015a 在unix集群上运行我的代码。
当我使用2.7.12在集群上运行我的代码时,我在pandas函数 to_datetime 中遇到错误,使用2.7.13不会发生错误。

当我尝试将数据框中的日期条目转换为日期时间时,会发生错误。

以下是使用to_datetime命令

得到的错误示例
df = pd.DataFrame({'year': [2015, 2016],
                       'month': [2, 3],
                       'day': [4, 5]})
pd.to_datetime(df)

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/easybuild/software/Python/2.7.12-goolf-
2015a/lib/python2.7/site-packages/pandas-0.16.1-py2.7-linux-
x86_64.egg/pandas/tseries/tools.py", line 343, in to_datetime
return _convert_listlike(arg, box, format)
File "/usr/local/easybuild/software/Python/2.7.12-goolf-
2015a/lib/python2.7/site-packages/pandas-0.16.1-py2.7-linux-
x86_64.egg/pandas/tseries/tools.py", line 266, in _convert_listlike
if com.is_datetime64_ns_dtype(arg):
File "/usr/local/easybuild/software/Python/2.7.12-goolf-
2015a/lib/python2.7/site-packages/pandas-0.16.1-py2.7-linux-
x86_64.egg/pandas/core/common.py", line 2513, in is_datetime64_ns_dtype
tipo = _get_dtype(arr_or_dtype)
File "/usr/local/easybuild/software/Python/2.7.12-goolf-
2015a/lib/python2.7/site-packages/pandas-0.16.1-py2.7-linux-
x86_64.egg/pandas/core/common.py", line 2458, in _get_dtype
return arr_or_dtype.dtype
File "/usr/local/easybuild/software/Python/2.7.12-goolf-
2015a/lib/python2.7/site-packages/pandas-0.16.1-py2.7-linux-
x86_64.egg/pandas/core/generic.py", line 2083, in __getattr__
(type(self).__name__, name))
AttributeError: 'DataFrame' object has no attribute 'dtype'

当我加载Python / 3.5.2-intel-2016.u3时,to_datetime正常工作,但这会导致其他问题。我对unix操作系统缺乏经验,而且我对集群上的软件没有管理员权限,我非常依赖于使用2.7.12

如果有人遇到过这个问题,并就如何解决这个问题提出了建议,那将非常感谢!

谢谢, 艾玛

2 个答案:

答案 0 :(得分:1)

尝试加入列,然后应用pd.to_datetime

pd.to_datetime(df.astype(str).apply('-'.join, 1))

0   2015-04-02
1   2016-05-03
dtype: datetime64[ns]

答案 1 :(得分:0)

DataFrame.dtypes是列出数据类型的属性,对于系列而言,它是dtype。

参考:https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.dtypes.html