Pandas 日期时间自定义错误:“系列对象不可调用”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42422366/
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-09-14 03:03:15  来源:igfitidea点击:

Pandas Datetime Customization Error: 'Series Object is not Callable'

pythonpandasdataframetime-series

提问by Cole Starbuck

I have a dataframe, Df originally from a CSV file:

我有一个数据框 Df 最初来自 CSV 文件:

     Ticker                    Date     Open     High      Low    Close  \
0     ES H7   10/18/2016 1:44:59 PM  2128.25  2128.50  2128.00  2128.00   
1     ES H7   10/18/2016 1:59:59 PM  2127.75  2129.25  2127.75  2128.75   
2     ES H7   10/18/2016 2:14:59 PM  2127.25  2127.25  2124.50  2125.75   
3     ES H7   10/18/2016 2:29:59 PM  2126.50  2126.50  2126.50  2126.50   
4     ES H7   10/18/2016 2:44:59 PM  2125.75  2126.75  2125.75  2126.50   
5     ES H7   10/18/2016 4:14:59 PM  2126.25  2126.25  2126.00  2126.00   
6     ES H7   10/18/2016 4:44:59 PM  2126.50  2126.50  2126.25  2126.25   
7     ES H7   10/18/2016 5:59:59 PM  2126.50  2126.50  2126.50  2126.50   
8     ES H7   10/18/2016 6:14:59 PM  2127.00  2127.00  2127.00  2127.00   
9     ES H7   10/18/2016 7:14:59 PM  2126.50  2127.75  2126.50  2127.75   

I am trying to make sure that the Date column is set to properly account for the date/time:

我试图确保日期列设置为正确说明日期/时间:

Df = pd.read_csv(file location)
Df = pd.DataFrame(Df)
pd.to_datetime(Df.Date('%m%d%y %H:%M:%S'))

That looks correct to me based on other posts, however I get the error for the datetime line:

根据其他帖子,这对我来说是正确的,但是我收到了日期时间行的错误:

Type Error: 'Series' object is not callable

Maybe I'm missing the AM/PM indication here and that's causing the error?

也许我在这里错过了 AM/PM 指示,这是导致错误的原因?

回答by languitar

Df.Datereturns a Seriescontaining the dates from your data frame. You cannot call this with a format string. I suspect you want to ensure that the data type of the column is datetime. This will convert it appropriately:

Df.Date返回Series包含数据框中日期的 。您不能使用格式字符串调用它。我怀疑您想确保列的数据类型是日期时间。这将适当地转换它:

In [4]: Df
Out[4]:
    Date  foo
0   9:40   42
1  13:30   13

In [5]: Df.Date = pd.to_datetime(Df.Date)

In [6]: Df
Out[6]:
                 Date  foo
0 2017-02-23 09:40:00   42
1 2017-02-23 13:30:00   13

回答by Alexey K.

languitar's answer is right here, pandas can guess many types of formats automatically:

languitar 的答案就在这里,pandas 可以自动猜测多种格式:

 pd.to_datetime(Df.Date)

if you need to specify the format, then you need to precisely formulate the full line; your case:

如果你需要指定格式,那么你需要精确地制定完整的行;你的情况:

pd.to_datetime(Df.Date, format='%m/%d/%Y %I:%M:%S %p')  
pd.to_datetime('10/18/2016 2:29:59 PM', format='%m/%d/%Y %I:%M:%S %p')  

(big Y, I, p and all separators) https://docs.python.org/3.4/library/datetime.html

(大 Y、I、p 和所有分隔符) https://docs.python.org/3.4/library/datetime.html

p.s. as a courtesy use a shorter example to pd.read_clipboard(sep='\s\s+')

ps 作为礼貌使用更短的例子 pd.read_clipboard(sep='\s\s+')

    Ticker             Date         Open
0   ES H7   10/18/2016 1:44:59 PM   2128.25
1   ES H7   10/18/2016 1:59:59 PM   2127.75

pd.read_csv()reads your file to DataFrame (no need pd.DataFrame(Df))

pd.read_csv()将您的文件读取到 DataFrame(不需要pd.DataFrame(Df)