Brian H. Truong
Brian H. Truong

Reputation: 53

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.

Upvotes: 0

Views: 342

Answers (3)

P Maschhoff
P Maschhoff

Reputation: 186

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')

Upvotes: 2

accdias
accdias

Reputation: 5372

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.

Upvotes: 0

cs95
cs95

Reputation: 403218

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')

Upvotes: 1

Related Questions