w3resource

Pandas Data Series: Create a TimeSeries to display all the Sundays of given year

 


35. Sundays TimeSeries

Write a Pandas program to create a TimeSeries to display all the Sundays of given year.

Sample Solution :

Python Code :

import pandas as pd
result = pd.Series(pd.date_range('2020-01-01', periods=52, freq='W-SUN'))
print("All Sundays of 2019:")
print(result)

Sample Output:

All Sundays of 2019:
0    2020-01-05
1    2020-01-12
2    2020-01-19
3    2020-01-26
4    2020-02-02
5    2020-02-09
6    2020-02-16
7    2020-02-23
8    2020-03-01
9    2020-03-08
10   2020-03-15
11   2020-03-22
12   2020-03-29
13   2020-04-05
14   2020-04-12
15   2020-04-19
16   2020-04-26
17   2020-05-03
18   2020-05-10
19   2020-05-17
20   2020-05-24
21   2020-05-31
22   2020-06-07
23   2020-06-14
24   2020-06-21
25   2020-06-28
26   2020-07-05
27   2020-07-12
28   2020-07-19
29   2020-07-26
30   2020-08-02
31   2020-08-09
32   2020-08-16
33   2020-08-23
34   2020-08-30
35   2020-09-06
36   2020-09-13
37   2020-09-20
38   2020-09-27
39   2020-10-04
40   2020-10-11
41   2020-10-18
42   2020-10-25
43   2020-11-01
44   2020-11-08
45   2020-11-15
46   2020-11-22
47   2020-11-29
48   2020-12-06
49   2020-12-13
50   2020-12-20
51   2020-12-27
dtype: datetime64[ns]

Explanation:

result = pd.Series(pd.date_range('2020-01-01', periods=52, freq='W-SUN'))

The above code creates a Pandas Series object 'result' containing 52 elements, where each element represents a Sunday from a range of dates starting from '2020-01-01' and extending to 52 weeks (i.e., one year) in the future. The dates are generated using the pd.date_range() function, which creates a range of dates with a specified frequency.

Here, the frequency is set to 'W-SUN', which indicates weekly frequency on Sundays. Therefore, the resulting series contains 52 dates that fall on Sundays, with one date per week.

Finally the resulting series is printed using the print() function.


For more Practice: Solve these Related Problems:

  • Write a Pandas program to create a TimeSeries listing all Saturdays of a given year.
  • Write a Pandas program to generate a TimeSeries of all weekend dates (Saturdays and Sundays) for a specified year.
  • Write a Pandas program to create a TimeSeries of all Sundays in a given year and format the dates in 'DD-MM-YYYY'.
  • Write a Pandas program to generate a TimeSeries of all Sundays in a leap year and validate the number of entries.

Python-Pandas Code Editor:

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

Previous: Write a Pandas program to compute the autocorrelations of a given numeric series.
Next: Write a Pandas program to convert given series into a dataframe with its index as another column on the dataframe.

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.