1

I have a dataframe (sample of which is shown below):

import pandas as pd
df = pd.DataFrame(data={'Date': ['Jan-1', 'Jan-1', 'Jan-4', 'Jan-4', 'Jan-5', 'Jan-6', 'Jan-6', 'Jan-6']})

i.e. :

  Date
0 Jan-1
1 Jan-1
2 Jan-4
3 Jan-4
4 Jan-5
5 Jan-6
6 Jan-6
7 Jan-6

I want to extract only the day part from it. which should return me as follows:

1
1
4
4
5
6
6
6

and what I am trying is as follows:

df['Date2'] = pd.to_datetime(df['Date'], format="%M-%d")

but this has resulted in error... not sure what I am doing wrong here. how to extract the day then, in my case?

rpanai
  • 12,515
  • 2
  • 42
  • 64
loveR
  • 489
  • 4
  • 12
  • Does this answer your question? [How to extract the year, month and day in Pandas?](https://stackoverflow.com/questions/41138190/how-to-extract-the-year-month-and-day-in-pandas) – Rakesh Jan 02 '20 at 12:10
  • No, Rakesh. Date format is different. and issue is more related to correct date format in Pandas on which the rest action can be performed. Thanks, anyways. – loveR Jan 02 '20 at 12:12

1 Answers1

3

First use %b for match name of month and then Series.dt.day:

df['Date2'] = pd.to_datetime(df['Date'], format="%b-%d").dt.day

Or if possible extract numbers and convert to integers:

df['Date2'] = df['Date'].str.extract('(\d+)').astype(int)

print (df)
    Date  Date2
0  Jan-1      1
1  Jan-1      1
2  Jan-4      4
3  Jan-4      4
4  Jan-5      5
5  Jan-6      6
6  Jan-6      6
7  Jan-6      6
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252