Extract week number from date in Pandas-Python
Many times, when working with some data containing dates we may need to extract the week number from a particular date. In Python, it can be easily done with the help of pandas.
0 25 Name: Date, dtype: int64
Example 2: We can also do the same for multiple dates by adding more dates in the ‘Date’ object.
Example 3: Extracting week number from dates for multiple dates using date_range() and to_series().
- pandas.data_range(): It generates all the dates from the start to end date
pandas.date_range(start, end, periods, freq, tz, normalize, name, closed)
- pandas.to_series(): It creates a Series with both index and values equal to the index keys.
Index.to_series(self, index, name)
Example 4: In this example, we’ll be using pandas.Series() to generate dates and use a different way to convert the series to the dataframe.
pandas.Series(): Used to create a one-dimensional ndarray with axis labels.
pandas.Series(data, index, dtype, name, copy, fastpath)
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course