Ragnar Lothbrok
Ragnar Lothbrok

Reputation: 1135

Convert Raw Date into Year / Month / Day of Week in Pandas

I have a Pandas dataframe with raw dates formatted as such "19990130". I want to convert these into new columns: 'year', 'month', and 'dayofweek'.

I tried using the following:

pd.to_datetime(df['date'], format='%Y%m%d', errors='ignore').values

Which does give me an array of datetime objects. However, the next step I tried was using .to_pydatetime() and then .year to try to get the year out, like this:

pd.to_datetime(df['date'], format='%Y%m%d', errors='ignore').values.to_pydatetime().year

This works when I test a single value, but with a Pandas dataframe. I get:

'numpy.ndarray' object has no attribute 'to_pydatetime'

What's the easiest way to extract the year, month, and day of week from this data?

Upvotes: 1

Views: 1726

Answers (1)

Quang Hoang
Quang Hoang

Reputation: 150735

Try:

s = pd.to_datetime(df['date'], format='%Y%m%d', errors='coerce')

s.dt.year
# or
# s.dt.month, etc

Upvotes: 2

Related Questions