Python Pandas - Format the Period object and display the Time with 24-Hour format

AmitDiwan
Updated on 26-Mar-2026 17:46:15

1K+ Views

To format the Period object and display time in 24-hour format, use the period.strftime() method with the %H parameter. The %H format code represents hours in 24-hour format (00-23). Creating a Period Object First, let's create a Pandas Period object with a specific date and time ? import pandas as pd # Create a Period object with second frequency period = pd.Period(freq="S", year=2021, month=9, day=18, hour=17, minute=20, second=45) print("Period object:") print(period) Period object: 2021-09-18 17:20:45 Formatting with 24-Hour Time Use strftime() with %H to extract the hour in ... Read More

Python Pandas - Format the Period object and display the Year without century

AmitDiwan
Updated on 26-Mar-2026 17:45:58

305 Views

To format a Period object in Pandas, use the period.strftime() method. To display the year without century, use the %y format parameter which shows only the last two digits of the year. Creating a Period Object First, import pandas and create a Period object with specific datetime components ? import pandas as pd # Create a Period object with frequency 'S' (second) period = pd.Period(freq="S", year=2021, month=9, day=18, hour=8, minute=20, second=45) print("Period object:", period) Period object: 2021-09-18 08:20:45 Formatting Year Without Century Use the strftime() method with the %y ... Read More

Python Pandas - Format the Period object and display Quarter

AmitDiwan
Updated on 26-Mar-2026 17:45:42

2K+ Views

To format the Period object in Pandas, use the period.strftime() method. To display the quarter, set the parameter as Q%q which shows the quarter number (Q1, Q2, Q3, or Q4). Creating a Period Object First, import pandas and create a Period object with specific date and time components ? import pandas as pd # Create a Period object with second frequency period = pd.Period(freq="S", year=2021, month=9, day=18, hour=8, minute=20, second=45) # Display the Period object print("Period...") print(period) Period... 2021-09-18 08:20:45 Formatting to Display Quarter Use strftime() with 'Q%q' ... Read More

Python Pandas - Format and return the string representation of the Period object

AmitDiwan
Updated on 26-Mar-2026 17:45:25

1K+ Views

To format and return the string representation of a Period object in Pandas, use the strftime() method. This method accepts format specifiers to customize the output format, such as '%d-%b-%Y' for day-month-year representation. What is a Period Object? A Pandas Period represents a specific time span with a defined frequency. It can represent anything from seconds to years, depending on the frequency parameter specified. Creating a Period Object First, let's create a Period object with second-level frequency ? import pandas as pd # Create a Period object with second frequency period = pd.Period(freq="S", ... Read More

Python Pandas - Change the frequency of the given Period object from Seconds to Daily frequency

AmitDiwan
Updated on 26-Mar-2026 17:45:06

616 Views

To change the frequency of a Pandas Period object from seconds to daily frequency, use the period.asfreq() method with the parameter 'D'. This conversion aggregates time information to the day level. Understanding Period Objects A Pandas Period represents a specific time span with an associated frequency. When you convert from a higher frequency (seconds) to a lower frequency (daily), the result retains only the date component ? import pandas as pd # Create a Period object with seconds frequency period = pd.Period(freq="S", year=2021, month=4, day=16, hour=2, minute=35, second=15) print("Original Period (seconds frequency):") print(period) print("Frequency:", period.freq) ... Read More

Python Pandas - Convert Period to desired frequency

AmitDiwan
Updated on 26-Mar-2026 17:44:48

663 Views

To convert Period to desired frequency, use the period.asfreq() method. This method allows you to change the frequency of a Pandas Period object to match your analysis requirements. What is asfreq()? The asfreq() method converts a Period from one frequency to another. When converting to a higher frequency (like daily to hourly), it uses specific rules to determine the exact timestamp within the period. Creating Period Objects First, let's create Period objects with different frequencies − import pandas as pd # Create Period objects with different frequencies period1 = pd.Period("2020-09-23 03:15:40") period2 = ... Read More

Python Pandas - Get the year from the Period object

AmitDiwan
Updated on 26-Mar-2026 17:44:30

602 Views

To get the year from the Period object, use the period.year property. A pandas Period represents a specific span of time with a given frequency. Importing Required Library First, import pandas to work with Period objects ? import pandas as pd Creating Period Objects You can create Period objects using different approaches ? import pandas as pd # Create Period from date string period1 = pd.Period("2020-09-23") # Create Period with specific frequency and date components period2 = pd.Period(freq="D", year=2021, month=4, day=16, hour=2, minute=35) print("Period1:", period1) print("Period2:", period2) ... Read More

Python Pandas - Get the week of the year on the given Period

AmitDiwan
Updated on 26-Mar-2026 17:44:15

299 Views

To get the week of the year on the given Period, use the period.weekofyear property. This property returns the week number (1-53) for any given date within a Period object. Syntax period.weekofyear Where period is a pandas Period object representing a specific date or time period. Creating Period Objects First, import the required library and create Period objects using different methods − import pandas as pd # Creating Period objects using different approaches period1 = pd.Period("2020-09-23") period2 = pd.Period(freq="D", year=2021, month=4, day=16, hour=2, minute=35) # Display the Period objects ... Read More

Python Pandas - Find the start time for the given Period object

AmitDiwan
Updated on 26-Mar-2026 17:43:59

389 Views

To find the start time for a given Period object, use the start_time property. A Pandas Period represents a specific time span, and start_time returns the exact timestamp when that period begins. What is start_time Property? The start_time property returns a Timestamp object representing the start of the period. This is useful when you need the precise beginning moment of a time period ? import pandas as pd # Create a Period object period = pd.Period("2020-09-22") print("Period:", period) print("Start time:", period.start_time) print("Type:", type(period.start_time)) Period: 2020-09-22 Start time: 2020-09-22 00:00:00 Type: ... Read More

Program to find out the largest sum value of a BST in a given binary tree in Python

Arnab Chakraborty
Updated on 26-Mar-2026 17:43:41

282 Views

Suppose we are provided a binary tree. We have to find out if there exist binary search trees (BST) in the subtrees of it and find out the sum of the largest BST. To find out the sum, we add the values of each node in that BST. We return the sum value as output. So, if the input is like ? then the output will be 12. The BST in the given binary tree is ? sum of the nodes = 12. Algorithm To solve this, we will follow ... Read More

Advertisements