Server Side Programming Articles - Page 814 of 2650

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

AmitDiwan
Updated on 20-Oct-2021 06:53:27

961 Views

To format the Period object, use the period.strftime() method and to display the time with 24-Hour format, set the parameter as %H.At first, import the required libraries −import pandas as pdThe pandas.Period represents a period of time. Creating a Period objectperiod = pd.Period(freq="S", year = 2021, month = 9, day = 18, hour = 17, minute = 20, second = 45)Display the Period objectprint("Period...", period) Display the result. Here, Time is displayed with 24-Hour format i.e. [00, 23]print("String representation (24-Hour format)...", period.strftime('%H'))ExampleFollowing is the code import pandas as pd # The pandas.Period represents a period of time # Creating ... Read More

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

AmitDiwan
Updated on 20-Oct-2021 06:51:41

262 Views

To format the Period object, use the period.strftime() method and to display the year without century, set the parameter as %y.At first, import the required libraries −import pandas as pdThe pandas.Period represents a period of time. Creating a Period objectperiod = pd.Period(freq="S", year = 2021, month = 9, day = 18, hour = 8, minute = 20, second = 45) Display the Period objectprint("Period...", period)Display the result. Here, year is displayed without centuryprint("String representation (year without century)...", period.strftime('%y')) ExampleFollowing is the code import pandas as pd # The pandas.Period represents a period of time # Creating a Period object ... Read More

Python Pandas - Format the Period object and display Quarter

AmitDiwan
Updated on 20-Oct-2021 06:47:46

2K+ Views

To format the Period object, use the period.strftime() method and to display Quarter, set the parameter as Q%q.At first, import the required libraries −import pandas as pdThe pandas.Period represents a period of time. Creating a Period objectperiod = pd.Period(freq="S", year = 2021, month = 9, day = 18, hour = 8, minute = 20, second = 45) Display the Period objectprint("Period...", period)Display the result. Here, Period object is formatted and Quarter is displayedprint("String representation (display quarter)...", period.strftime('Q%q')) ExampleFollowing is the code import pandas as pd # The pandas.Period represents a period of time # Creating a Period object period ... Read More

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

AmitDiwan
Updated on 20-Oct-2021 06:44:50

1K+ Views

To format and return the string representation of the Period object, use the period.strftime() method. With that, set the format specifiers as an argument like strftime('%d-%b-%Y').At first, import the required libraries −import pandas as pdThe pandas.Period represents a period of time. Creating a Period objectperiod = pd.Period(freq="S", year = 2021, month = 9, day = 18, hour = 8, minute = 20, second = 45) Display the Period objectprint("Period...", period)Display the formatted string representationprint("String representation (format)...", period.strftime('%d-%b-%Y')) ExampleFollowing is the code import pandas as pd # The pandas.Period represents a period of time # Creating a Period object period ... Read More

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

AmitDiwan
Updated on 20-Oct-2021 06:43:31

218 Views

To change the frequency of the given Period object from Seconds to Minutely frequency, use the period.asfreq() method and set the parameter ‘T’.At first, import the required libraries −import pandas as pdThe pandas.Period represents a period of time. Creating a Period object. We have set the frequency as seconds i.e. 'S' using the 'freq' parameter period = pd.Period(freq="S", year = 2021, month = 9, day = 11, hour = 8, minute = 20, second = 45)Display the Period object with Seconds frequencyprint("Period...", period) Convert Period from Seconds to Minutely frequency. We have set the "T" to convert seconds to minutely ... Read More

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

AmitDiwan
Updated on 20-Oct-2021 06:41:19

741 Views

To change the frequency of the given Period object from Seconds to Hourly frequency, use the period.asfreq() method and set the parameter ‘H’.At first, import the required libraries −import pandas as pdThe pandas.Period represents a period of time. Creating a Period object. We have set the frequency as seconds ie. 'S' using the 'freq' parameterperiod = pd.Period(freq="S", year = 2021, month = 4, day = 16, hour = 2, minute = 35, second = 15) Display the Period object with Seconds frequencyprint("Period...", period)Convert Period from Seconds to Hourly frequency. We have set the "H" to convert seconds to hourly frequency ... Read More

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

AmitDiwan
Updated on 20-Oct-2021 06:39:16

553 Views

To change the frequency of the given Period object from Seconds to Daily frequency, use the period.asfreq() method and set the parameter ‘D’.At first, import the required libraries −import pandas as pdThe pandas.Period represents a period of time. Creating a Period object. We have set the frequency as seconds ie. 'S' using the 'freq' parameterperiod = pd.Period(freq="S", year = 2021, month = 4, day = 16, hour = 2, minute = 35, second = 15)Display the Period object with Seconds frequencyprint("Period...", period) Convert Period from Seconds to Daily frequency. We have set the "D" to convert seconds to daily frequency ... Read More

Python Pandas - Convert Period to desired frequency

AmitDiwan
Updated on 20-Oct-2021 06:38:06

602 Views

To convert Period to desired frequency, use the period.asfreq() method. Let’s say we will set to desired Hourly frequency using the ‘H’ specifier.At first, import the required libraries −import pandas as pdThe pandas.Period represents a period of time. Create two Period objectsperiod1 = pd.Period("2020-09-23 03:15:40") period2 = pd.Period(freq="D", year = 2021, month = 4, day = 16, hour = 2, minute = 35)Display the Period objectsprint("Period1...", period1) print("Period2...", period2)Convert Period to desired frequency. We have set frequency as H i.e. Hourly frequencyres1 = period1.asfreq('H') res2 = period2.asfreq('H')ExampleFollowing is the code import pandas as pd # The pandas.Period represents a ... Read More

Python Pandas - Get the year from the Period object

AmitDiwan
Updated on 20-Oct-2021 06:34:31

550 Views

To get the year from the Period object, use the period.year property. At first, import the required libraries −import pandas as pdThe pandas.Period represents a period of time. Creating two Period objectsperiod1 = pd.Period("2020-09-23") period2 = pd.Period(freq="D", year = 2021, month = 4, day = 16, hour = 2, minute = 35)Display the Period objectsprint("Period1...", period1) print("Period2...", period2)Get the year from two Period objectsres1 = period1.year res2 = period2.yearExampleFollowing is the code import pandas as pd # The pandas.Period represents a period of time # creating two Period objects period1 = pd.Period("2020-09-23") period2 = pd.Period(freq="D", year = 2021, month ... Read More

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

AmitDiwan
Updated on 20-Oct-2021 06:29:43

227 Views

To get the week of the year on the given Period, use the period.weekofyear property. At first, import the required libraries −import pandas as pdThe pandas.Period represents a period of time. Creating two Period objectsperiod1 = pd.Period("2020-09-23") period2 = pd.Period(freq="D", year = 2021, month = 4, day = 16, hour = 2, minute = 35)Display the Period objectsprint("Period1...", period1) print("Period2...", period2)Get the week of the year from two Period objectsres1 = period1.weekofyear res2 = period2.weekofyearExampleFollowing is the code import pandas as pd # The pandas.Period represents a period of time # creating two Period objects period1 = pd.Period("2020-09-23") period2 ... Read More

Advertisements