Home > Net >  Extracting month from date in dd/mm/yyyy format in pandas
Extracting month from date in dd/mm/yyyy format in pandas

Time:05-10

I can't get month and day from date in the correct format.

I'm using both pd.DatetimeIndex(df['date1']).month and pd.to_datetime(parity['date1']).dt.month but it still retrieves day as month and only if value is larger than 12 it considers it as day. Thank you in advance

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

CodePudding user response:

Specify format of dates:

df['date1'] = pd.to_datetime(df['date1'], format='%d.%m.%Y').dt.month 

Or set parameter dayfirst=True:

df['date1'] = pd.to_datetime(df['date1'], dayfirst=True).dt.month 
  • Related