简体   繁体   English

“AttributeError: 'DataFrame' 对象没有属性 'dtype'” 使用 Pandas to_datetime

[英]"AttributeError: 'DataFrame' object has no attribute 'dtype'" using pandas to_datetime

I have been using Python 2.7.13 on a windows machine to write my code.我一直在 Windows 机器上使用Python 2.7.13来编写我的代码。 But I am now trying to run my code on a unix cluster using Python 2.7.12-goolf-2015a .但我现在正在尝试使用 Python 2.7.12-goolf-2015a在 unix 集群上运行我的代码。
When I run my code on the cluster with 2.7.12, I encounter an error in the pandas function to_datetime which does not occur using 2.7.13.当我使用 2.7.12 在集群上运行我的代码时,我在 pandas 函数to_datetime 中遇到一个错误,使用 2.7.13 不会发生该错误。

The error occurs when I try to convert a date entries in a dataframe to datetime.当我尝试将数据框中的日期条目转换为日期时间时发生错误。

Here is an example ot the error I get with the to_datetime command这是我使用 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'

The to_datetime works when I load Python/3.5.2-intel-2016.u3, but that causes other problems.当我加载 Python/3.5.2-intel-2016.u3 时,to_datetime 有效,但这会导致其他问题。 I am inexperienced with unix operating systems and as I don't have admin rights to software on the cluster, I am pretty tied to using 2.7.12我对 unix 操作系统缺乏经验,并且由于我没有集群上软件的管理员权限,我非常依赖于使用 2.7.12

If anyone has encountered this issue and has advice on how to resolve it, that would be greatly appreciated!如果有人遇到过这个问题并有解决方法的建议,那将不胜感激!

Thanks, Emma谢谢,艾玛

Try joining the columns and then applying pd.to_datetime .尝试加入列,然后应用pd.to_datetime

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

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

DataFrame.dtypes is an attribute to list data types, for series it's a dtype. DataFrame.dtypes 是列出数据类型的属性,对于系列,它是一个 dtype。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AttributeError:&#39;DataFrame&#39;对象没有属性&#39;to_datetime&#39; - AttributeError: 'DataFrame' object has no attribute 'to_datetime' Python Pandas to_datetime AttributeError: 'tuple' object 没有属性 'lower' - Python Pandas to_datetime AttributeError: 'tuple' object has no attribute 'lower' AttributeError:&#39;str&#39;对象没有属性&#39;to_datetime&#39; - AttributeError: 'str' object has no attribute 'to_datetime' Pandas AttributeError: &#39;DataFrame&#39; 对象没有属性 &#39;Datetime&#39; - Pandas AttributeError: 'DataFrame' object has no attribute 'Datetime' 理解 to_datetime AttributeError: &#39;tuple&#39; 对象没有属性 &#39;lower&#39; - Understand to_datetime AttributeError: 'tuple' object has no attribute 'lower' AttributeError:“ DataFrame”对象没有属性“ datetime” - AttributeError: 'DataFrame' object has no attribute 'datetime' AttributeError: 'DataFrame' object 没有属性 'get_dtype_counts' - AttributeError: 'DataFrame' object has no attribute 'get_dtype_counts' 熊猫read_csv:AttributeError:“ NoneType”对象没有属性“ dtype” - Pandas read_csv: AttributeError: 'NoneType' object has no attribute 'dtype' “系列”对象没有属性“ to_datetime” - 'Series' object has no attribute 'to_datetime' 熊猫:AttributeError:&#39;DataFrame&#39;对象没有属性&#39;agg&#39; - Pandas: AttributeError: 'DataFrame' object has no attribute 'agg'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM