简体   繁体   English

Pandas “to_datetime” 不接受系列

[英]Pandas “to_datetime” not accepting series

I am new to pandas and am trying to convert a column of strings with dates in the format '%d %B' (01 January, 02 January.... ) to date time objects and the type of the column is <class 'pandas.core.series.Series'> .我是 pandas 的新手,我正在尝试将日期格式为“%d %B”(1 月 1 日、1 月 2 日 ....)的字符串列转换为日期时间对象,并且该列的类型是<class 'pandas.core.series.Series'> if i pass in this series in the to_datetime method, like如果我在 to_datetime 方法中传递这个系列,比如

print(pd.to_datetime(data_file['Date'], format='%d %B', errors="coerce"))

it all returns NaT for all the entries, where as it should return date time objects它都返回所有条目的NaT ,它应该返回日期时间对象

I checked the documentation and it says that it accepts a Series object.我检查了文档,它说它接受 object 系列。

Any way to fix this?有任何解决这个问题的方法吗?

Edit 1: here is the head of the data i am using:编辑1:这是我正在使用的数据的负责人:

           Date  Daily Confirmed
0   30 January                 1
1   31 January                 0
2  01 February                 0
3  02 February                 1
4  03 February                 1

edit 2: here is the information of the data编辑2:这是数据的信息

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 179 entries, 0 to 178
Data columns (total 2 columns):
 #   Column           Non-Null Count  Dtype 
---  ------           --------------  ----- 
 0   Date             179 non-null    object
 1   Daily Confirmed  179 non-null    int64 
dtypes: int64(1), object(1)
memory usage: 2.2+ KB

If I understand correctly, you may be facing this issue because there are spaces around the dates in this column.如果我理解正确,您可能会遇到此问题,因为此列中的日期周围有空格。 To solve it, use strip before to_datetime .要解决它,请在to_datetime之前使用strip Here's a piece of code that does that:这是一段代码:

df = pd.DataFrame({'Date': 
                   ['30 January ', '31 January ', ' 01 February ', '02 February', 
                    '03 February'], 'Daily Confirmed': [1, 0, 0, 1, 1]})

pd.to_datetime(df.Date.str.strip(), format = "%d %B")

The output is: output 是:

0   1900-01-30
1   1900-01-31
2   1900-02-01
...
import pandas as pd 
dic =  {"Date": ["30 January", "31 January", "01 February", ] ,  "Daily Confirmed":[0,1,0]}
df =pd.DataFrame(dic)
df['date1'] = pd.to_datetime(df['Date'].astype(str), format='%d %B')
df

By default, it contains years as 1900. Because you did not provide year on your Dataframe Output:默认情况下,它包含 1900 年。因为您没有在 Dataframe Output 上提供年份:

    Date       Daily Confirmed  date1
0   30 January      0          1900-01-30
1   31 January      1          1900-01-31
2   01 February     0          1900-02-01

If you don't want year as prefix of date.如果您不希望年份作为日期的前缀。 Please add the below code:请添加以下代码:

df['date2']=df['date1'].dt.strftime('%d-%m')
df
    Date       Daily Confirmed  date1         date2
0   30 January      0          1900-01-30     30-1
1   31 January      1          1900-01-31     31-1
2   01 February     0          1900-02-01     01-2

Thanks谢谢

You may try this:你可以试试这个:

from datetime import datetime

df['datetime'] = df['date'].apply(lambda x: datetime.strptime(x, "%d %B"))

apply() allows you to use python functions in series, here you may have to specify the year otherwise the default year (1900) will be set as default. apply()允许您串联使用 python 函数,在这里您可能必须指定年份,否则默认年份 (1900) 将被设置为默认值。

Good luck祝你好运

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM