I have a non-index column in a python dataframe with a date like 02/03/2017. I would like to extract the day of the week and make it a separate column.
Asked
Active
Viewed 3,898 times
-1
-
1Well what have you tried thus far? – Denziloe Mar 08 '17 at 20:01
-
*Dataframe* is a generic term. Would you care to identify the specific software system you're using? – Bill Bell Mar 08 '17 at 20:28
-
Sorry. I'm using pandas in python 3. – kozowh Mar 08 '17 at 20:50
-
I should also mention that the errors I get refer to trying to process the column as a series which doesn't seem to be allowed. For – kozowh Mar 08 '17 at 21:07
-
For example, if I write: df['DOW'] = datetime.datetime.strptime(df[Production_Date], '%mm/%m/%Y') I get an error like Can't parse two arguments of types. I'm just grasping at straws here. – kozowh Mar 08 '17 at 21:08
-
I didn't see your reply until just now. When you want to respond to someone the safest thing to do is to put @name somewhere in your comment. For instance, to respond to me, you would put (at)Bill Bell in your comment. Then what you write appears as a number on the other person's SO display. – Bill Bell Mar 13 '17 at 23:08
4 Answers
3
Actually there is solution using pandas.
import pandas as pd
your_df = pd.DataFrame(data={'Date': ['31/1/2018', '1/1/2018', '31/12/2018',
'28/2/2016', '3/3/2035']})
your_df['Date'] = pd.to_datetime(your_df['Date'], format="%d/%m/%Y")
your_df['Day of week (int)'] = your_df['Date'].dt.weekday
your_df['Day of week (str)'] = your_df['Date'].dt.day_name()
print(your_df)
More info here: Create a day-of-week column in a Pandas dataframe using Python
Notes as per my other (less elegant) answer...

axb2035
- 63
- 5
2
You might also be interested in the arrow module since it offers quite a few features and advantages. Here I demonstrate its ability to provide weekday names in two forms for one locale, and in one form for a non-English locale.
>>> import arrow
>>> theDate = arrow.get('02/03/2017', 'DD/MM/YYYY')
>>> theDate
<Arrow [2017-03-02T00:00:00+00:00]>
>>> theDate.weekday()
3
>>> theDate.format('ddd', locale='en_GB')
'Thu'
>>> theDate.format('dddd', locale='en_GB')
'Thursday'
>>> theDate.format('dddd', locale='fr_FR')
'jeudi'

Bill Bell
- 21,021
- 5
- 43
- 58
-
Thanks for the suggestion. I have to see if I can apply it to a pandas dataframe column. – kozowh Mar 13 '17 at 20:23
1
First you need to convert the date to a datetime object:
import datetime
date = datetime.datetime.strptime("02/03/2017", "%d/%m/%Y")
print date.weekday()
See https://docs.python.org/2/library/datetime.html#module-datetime

ScottSmudger
- 351
- 2
- 8
0
The solution I've found is a two step process as I haven't been able to find a way to get weekday()
work on a pandas series.
import pandas as pd
your_df = pd.DataFrame(data={'Date': ['31/1/2018', '1/1/2018', '31/12/2018',
'28/2/2016', '3/3/2035']})
dt_series = pd.to_datetime(your_df['Date'], format="%d/%m/%Y")
dow = []
for dt in range(0, len(your_df)):
dow.append(dt_series[dt].weekday())
your_df.insert(1, 'Day of week', dow)
print(your_df)
The output should look like this:
Date Day of week
0 31/1/2018 2
1 1/1/2018 0
2 31/12/2018 0
3 28/2/2016 6
4 3/3/2035 5
Notes:
- I'm using dd/mm/yyyy format. You will need to change the format argument for
to_datetime()
if your dates are in U.S. or other formats. - Python weekdays: Monday = 0, Sunday = 6.

axb2035
- 63
- 5