w3resource

Pandas Data Series: Get the day of month, year, week, week number

 


28. Extract Date Components

Write a Pandas program to get the day of month, day of year, week number and day of week from a given series of date strings.

Sample Solution :

Python Code :

import pandas as pd
from dateutil.parser import parse
date_series = pd.Series(['01 Jan 2015', '10-02-2016', '20180307', '2014/05/06', '2016-04-12', '2019-04-06T11:20'])
print("Original Series:")
print(date_series)
date_series = date_series.map(lambda x: parse(x))
print("Day of month:")
print(date_series.dt.day.tolist())
print("Day of year:")
print(date_series.dt.dayofyear.tolist())
print("Week number:")
print(date_series.dt.weekofyear.tolist())
print("Day of week:")
print(date_series.dt.weekday_name.tolist())

Sample Output:

Original Series:
0         01 Jan 2015
1          10-02-2016
2            20180307
3          2014/05/06
4          2016-04-12
5    2019-04-06T11:20
dtype: object
Day of month:
[1, 2, 7, 6, 12, 6]
Day of year:
[1, 276, 66, 126, 103, 96]
Week number:
[1, 39, 10, 19, 15, 14]
Day of week:
['Thursday', 'Sunday', 'Wednesday', 'Tuesday', 'Tuesday', 'Saturday']

Explanation:

date_series = pd.Series(['01 Jan 2015', '10-02-2016', '20180307', '2014/05/06', '2016-04-12', '2019-04-06T11:20']): This code creates a Pandas Series object 'date_series' containing six strings representing dates in different formats.

date_series = date_series.map(lambda x: parse(x)): This line applies the map() method to the Pandas Series object 'date_series' and a lambda function to parse each string into a Pandas Timestamp object using the dateutil.parser.parse() method.

The following code lines then apply various Pandas datetime-related methods to the Pandas Series object 'date_series'.

  • date_series.dt.day.tolist() returns a list of the day-of-the-month values of the Timestamp objects in 'date_series'.
  • date_series.dt.dayofyear.tolist() returns a list of the day-of-the-year values of the Timestamp objects in 'date_series'.
  • date_series.dt.weekofyear.tolist() returns a list of the week-of-the-year values of the Timestamp objects in 'date_series'.
  • date_series.dt.weekday_name.tolist() returns a list of the weekday names (i.e., Monday, Tuesday, etc.) of the Timestamp objects in 'date_series'.

For more Practice: Solve these Related Problems:

  • Write a Pandas program to extract the month and year from a Series of date strings and add them as new columns.
  • Write a Pandas program to extract the day, week, and quarter from a Series of date strings.
  • Write a Pandas program to convert a Series of date strings to datetime and then extract the day of the week in full text.
  • Write a Pandas program to extract date components (day, month, year) from a Series and then filter by specific months.

Go to:


Previous: Write a Pandas program to convert a series of date strings to a timeseries.
Next: Write a Pandas program to convert year-month string to dates adding a specified day of the month.

Python-Pandas Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.