1

I am reading data from a csv file, and I have a Date column formatted as "dd/mm/yyyy".

I just want to extract the month from it.

The problem is that in some cases (based on my observation it is when dd > 12), it returns the month correctly. Otherwise, it returns the day instead.

I tried extracting the day assuming that maybe the built-in format is "mm/dd/yyyy", but the same problem occurs. Only the year gets extracted correctly.

I have tried reformatting the date by replacing "/" with "-" but it did not solve the issue.

This is the code for extracting the month:

pd.DatetimeIndex(df['Date']).month

Here is an example of the results I'm obtaining.

Date: 19-01-2012,
Month: 01

Date: 04-01-2012,
Month: 04

tawab_shakeel
  • 3,701
  • 10
  • 26
Nour
  • 197
  • 1
  • 3
  • 13

2 Answers2

3

One thing you can do is recast the df['Date'] column as a datetime.

You can specify that the days are first with the dayfirst parameter.

df['Date'] = pd.to_datetime(df['Date'], dayfirst=True)

source: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.to_datetime.html

When I use this with your example, and extract the months with:

pd.DatetimeIndex(df['Date']).month

I see both months as 1 for January.

DKAFRN
  • 41
  • 3
1

try adding the format="%d-%m-%Y:

df = pd.DataFrame(data={'date':["19-01-2012","04-01-2012"]})

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

print(df)
       date     month
0  19-01-2012      1
1  04-01-2012      1

I hope it would solve your problem

tawab_shakeel
  • 3,701
  • 10
  • 26