Python Pandas Period.day Attribute

Python is an excellent language for data analysis, thanks to its vast ecosystem of data-centric Python packages. One of these packages is Pandas, which makes importing and analysing data a lot easier.

Pandas

Pandas is a Python open-source library that provides high-performance data manipulation. Pandas gets its name from the term Panel Data, which means Econometrics from Multidimensional data. Wes McKinney created it in 2008 for data analysis in Python.

Data analysis necessitates a significant amount of processing, such as restructuring, cleaning, or merging. Numpy, Scipy, Cython, and Panda are some tools for fast data processing. But we prefer Pandas because they are faster, simpler, and more expressive than other tools.

Pandas is built on top of the Numpy package, so Numpy is required to run Pandas.

Period in Pandas:

The Time Periods represent the length of time, such as days, years, quarters, and months. It’s a class that lets us convert frequencies to periods.

Period.day Pandas:

For the given Period object, the day attribute returns the month’s day. It returns the  integer value.

Syntax :

Period.day

Parameters:

It has no arguments/parameters.

Return :

It returns the day number in the corresponding month and year.

Pandas Period.day Attribute in Python

We can also pass freq = H( hourly frequency), T(minutely), S(secondly), D(Day), Q(quaterly) etc.

Example1

To find the day of the month in the given object, we’ll use the Period. day attribute.

Approach:

  • Import pandas module using the import keyword.
  • Pass some random frequency(here D=Day), year, month, days as the arguments to the Period() function of the pandas module to get the Period object
  • Print the given period object
  • Apply the day attribute on the above period object to get the start time of the given period object.
  • The Exit of the Program.

Below is the implementation:

# Import pandas module using the import keyword.
import pandas as pd
# Pass some random frequency(here D=Day), year, month, day,
# as the arguments to the Period() function
# of the pandas module to get the Period object
period_obj = pd.Period(freq ='D', year = 2020, month = 9, day = 28)
  
# Print the given period object
print("The given period object:")
print(period_obj)
print()
# Apply day() attribute on the above period object to get the 
# day value in the above period object
print("The day value of the given period object:")
print(period_obj.day)

Output:

The given period object:
2020-09-28

The day value of the given period object:
28

Explanation:

The Period. day attribute returned 28 in the output, which is the value of the day in the given period object.

Example2

To find the day value by passing the quarter value, we’ll use the Period. day attribute.

Approach:

  • Import pandas module using the import keyword.
  • Pass some random frequency(here Q=quaterly), year, quarter as the arguments to the Period() function of the pandas module to get the Period object
  • Print the given period object
  • Apply day() attribute on the above period object to get the day value in the quarter.
  • The Exit of the Program.

Below is the implementation:

# Import pandas module using the import keyword.
import pandas as pd
# Pass some random frequency(here Q=quaterly), year, quarter
# as the arguments to the Period() function
# of the pandas module to get the Period object
period_obj = pd.Period(freq ='Q', year = 2021,quarter =2)
  
# Print the given period object
print("The given period object:")
print(period_obj)
print()
# Apply day() attribute on the above period object to get the 
# day value in the quarter
print("The day value of the given quarter:")
print(period_obj.day)

Output:

The given period object:
2021Q2

The day value of the given quarter:
30

Explanation:

Here it prints the last day of the second quarter.