简体   繁体   中英

Python pandas date time, how to convert these dates to pandas datetime?

My dates are of the form Month, Year:

82013

102013

But I want them to be regular pandas datetime. When I insert these dates into pd.datetime I get

1970-01-01 00:00:00.000082013

This is very wrong.

Advice would be greatly appreciated thanks.

As roganjosh said, it would be better if you could just get your dates in an easier format in the first place. However, if you're stuck with this, can get by with just telling pd.to_datetime the format you expect.

dates = pd.Series(['82013', '102013'])
pd.to_datetime(dates, format='%m%Y')
df

     date
0   82013
1  102013

First, extract the month and year as separate columns using str.extract :

u = df.date.astype(str).str.extract(r'^(?P<month>\d{1,2})(?P<year>\d{4})$', expand=True)

  month  year
0     8  2013
1    10  2013

Now, let pd.to_datetime take over.

pd.to_datetime(u.assign(day=1))

0   2013-08-01
1   2013-10-01
dtype: datetime64[ns]

If invalid values are possible, use

pd.to_datetime(u.assign(day=1), errors='coerce')

Use Python's standard datetime module:

Python 3.7.2 (default, Jan 16 2019, 19:49:22) 
[GCC 8.2.1 20181215 (Red Hat 8.2.1-6)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from datetime import datetime
>>> datetime.strptime('82013', '%m%Y')
datetime.datetime(2013, 8, 1, 0, 0)
>>> datetime.strptime('102013', '%m%Y')
datetime.datetime(2013, 10, 1, 0, 0)
>>>

Dates generated that way are going to be set to the first day of the month at zero hour and zero minutes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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