Python AttributeError:只能使用具有类似日期时间的值的 .dt 访问器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33365055/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 13:14:35  来源:igfitidea点击:

AttributeError: Can only use .dt accessor with datetimelike values

pythonpandas

提问by Nasia Ntalla

Hi I am using pandas to convert a column to month. When I read my data they are objects:

嗨,我正在使用熊猫将一列转换为月份。当我读取我的数据时,它们是对象:

Date           object
dtype: object

So I am first making them to date time and then try to make them as months:

所以我首先将它们设置为日期时间,然后尝试将它们设置为月份:

import pandas as pd
file = '/pathtocsv.csv'
df = pd.read_csv(file, sep = ',', encoding='utf-8-sig', usecols= ['Date', 'ids'])    
df['Date'] = pd.to_datetime(df['Date'])
df['Month'] = df['Date'].dt.month

Also if that helps:

此外,如果这有帮助:

In [10]: df['Date'].dtype
Out[10]: dtype('O')

So, the error I get is like this:

所以,我得到的错误是这样的:

/Library/Frameworks/Python.framework/Versions/2.7/bin/User/lib/python2.7/site-packages/pandas/core/series.pyc in _make_dt_accessor(self)
   2526             return maybe_to_datetimelike(self)
   2527         except Exception:
-> 2528             raise AttributeError("Can only use .dt accessor with datetimelike "
   2529                                  "values")
   2530 

AttributeError: Can only use .dt accessor with datetimelike values

EDITED:

编辑:

Date columns are like this:

日期列是这样的:

0         2014-01-01         
1         2014-01-01         
2         2014-01-01         
3         2014-01-01         
4         2014-01-03       
5         2014-01-03         
6         2014-01-03         
7         2014-01-07         
8         2014-01-08         
9         2014-01-09 

Do you have any ideas? Thank you very much!

你有什么想法?非常感谢!

采纳答案by EdChum

Your problem here is that to_datetimesilently failed so the dtype remained as str/object, if you set param errors='coerce'then if the conversion fails for any particular string then those rows are set to NaT.

您的问题是to_datetime静默失败,因此 dtype 保持为str/object,如果您设置 paramerrors='coerce'那么如果任何特定字符串的转换失败,那么这些行将设置为NaT.

df['Date'] = pd.to_datetime(df['Date'], errors='coerce')

So you need to find out what is wrong with those specific row values.

因此,您需要找出这些特定行值有什么问题。

See the docs

查看文档

回答by Danny Wu

Your problem here is that the dtype of 'Date' remained as str/object. You can use the parse_datesparameter when using read_csv

您的问题是“日期”的 dtype 仍为 str/object。使用parse_dates时可以使用该参数read_csv

import pandas as pd
file = '/pathtocsv.csv'
df = pd.read_csv(file, sep = ',', parse_dates= [col],encoding='utf-8-sig', usecols= ['Date', 'ids'],)    
df['Month'] = df['Date'].dt.month

From the documentation for the parse_datesparameter

来自参数的文档parse_dates

parse_dates: bool or list of int or names or list of lists or dict, default False

The behavior is as follows:

  • boolean. If True -> try parsing the index.
  • list of int or names. e.g. If [1, 2, 3] -> try parsing columns 1, 2, 3 each as a separate date column.
  • list of lists. e.g. If [[1, 3]] -> combine columns 1 and 3 and parse as a single date column.
  • dict, e.g. {‘foo' : [1, 3]} -> parse columns 1, 3 as date and call result ‘foo'

If a column or index cannot be represented as an array of datetimes, say because of an unparseable value or a mixture of timezones, the column or index will be returned unaltered as an object data type. For non-standard datetime parsing, use pd.to_datetimeafter pd.read_csv. To parse an index or column with a mixture of timezones, specify date_parserto be a partially-applied pandas.to_datetime()with utc=True. See Parsing a CSV with mixed timezones for more.

Note: A fast-path exists for iso8601-formatted dates.

parse_dates: bool 或 int 或名称列表或列表或字典列表,默认为 False

行为如下:

  • 布尔值。如果为 True -> 尝试解析索引。
  • int 或名称列表。例如,如果 [1, 2, 3] -> 尝试将第 1、2、3 列解析为单独的日期列。
  • 列表列表。例如,如果 [[1, 3]] -> 合并第 1 列和第 3 列并解析为单个日期列。
  • dict,例如 {'foo' : [1, 3]} -> 将第 1、3 列解析为日期并调用结果 'foo'

如果列或索引无法表示为日期时间数组,例如由于无法解析的值或时区的混合,则该列或索引将作为对象数据类型原封不动地返回。对于非标准日期时间解析,请使用pd.to_datetimeafter pd.read_csv。要解析具有混合时区的索引或列,请指定date_parser为部分应用pandas.to_datetime()utc=True. 有关更多信息,请参阅解析具有混合时区的 CSV。

注意:iso8601 格式的日期存在快速路径。

The relevant case for this question is the "list of int or names" one.

这个问题的相关案例是“整数或名称列表”。

col is the columns index of 'Date' which parses as a separate date column.

col 是 'Date' 的列索引,它被解析为一个单独的日期列。