Create a Pandas TimeSeries to display all the Sundays of given year
Suppose we need to find out all the Sundays in the year 2020. Then we use the panda module for that. If we need to find the time-series to display all Sundays, we use panda. Series() class, which is a 1-D labeled array capable of holding any data.
Syntax: pandas.Series(data=None, index=None, dtype=None, name=None, copy=False, fastpath=False)
Return: Series object
For knowing more about the panda series click here.
Also, we use panda’s data_range() function for specifying our exact need. This method is used as a general function that returns a fixed frequency DatetimeIndex.
Syntax: pandas.date_range(start=None, end=None, periods=None, freq=None, tz=None, normalize=False, name=None, closed=None, **kwargs)
Return: Return a fixed frequency DatetimeIndex.
For digging more into the data_range() function click here.
Now, we will see the program.
Code:
Python3
# import pandas library import pandas as pd # create s series of all sundays result = pd.Series(pd.date_range( '2020-01-01' , periods = 52 , freq = 'W-SUN' )) print ( "All Sundays in 2020:" ) # show the series print (result) |
Output:
All Sundays in 2020: 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]