1

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?

Ragnar Lothbrok
  • 1,045
  • 2
  • 16
  • 31
  • Possible duplicate of [How do I convert dates in a Pandas data frame to a 'date' data type?](https://stackoverflow.com/questions/16852911/how-do-i-convert-dates-in-a-pandas-data-frame-to-a-date-data-type) – abhilb Sep 23 '19 at 19:47

1 Answers1

2

Try:

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

s.dt.year
# or
# s.dt.month, etc
Quang Hoang
  • 146,074
  • 10
  • 56
  • 74