Python Articles

Page 308 of 855

Python Pandas - Get the number of days from TimeDelta

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 9K+ Views

To get the number of days from a TimeDelta object in Pandas, use the timedelta.days property. This property returns only the day component as an integer, excluding hours, minutes, and seconds. Syntax timedelta.days Creating a TimeDelta Object First, create a TimeDelta object using pd.Timedelta() ? import pandas as pd # Create a Timedelta object timedelta = pd.Timedelta('5 days 1 min 45 s') print("Timedelta:", timedelta) Timedelta: 5 days 00:01:45 Extracting Days Use the .days property to get only the number of days ? import ...

Read More

Python Pandas - Return a components namedtuple-like

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 195 Views

Pandas provides the timedelta.components property to return a components namedtuple-like object that breaks down a Timedelta into its individual time units (days, hours, minutes, seconds, etc.). Basic Usage First, let's import pandas and create a Timedelta object ? import pandas as pd # Create a Timedelta object timedelta = pd.Timedelta('1 days 10 min 20 s') print("Timedelta:", timedelta) # Return a components namedtuple-like components = timedelta.components print("Components:", components) Timedelta: 1 days 00:10:20 Components: Components(days=1, hours=0, minutes=10, seconds=20, milliseconds=0, microseconds=0, nanoseconds=0) Accessing Individual Components You can access each component individually ...

Read More

Python - Check if the Pandas Index with some NaNs is a floating type

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 219 Views

To check if a Pandas Index with NaN values is a floating type, use the index.is_floating() method. This method returns True if the index contains floating-point numbers, even when NaN values are present. Syntax index.is_floating() This method returns a boolean value indicating whether the index is of floating-point type. Creating an Index with NaN Values First, let's create a Pandas index containing floating-point numbers and NaN values − import pandas as pd import numpy as np # Creating Pandas index with some NaNs index = pd.Index([5.7, 6.8, 10.5, np.nan, 17.8, ...

Read More

Python Pandas - Return a numpy timedelta64 array scalar view in nanoseconds

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 329 Views

To return a numpy timedelta64 array scalar view in nanoseconds, use the timedelta.asm8 property in Pandas. This property provides a direct numpy representation of the timedelta object. At first, import the required libraries − import pandas as pd Understanding Timedelta and asm8 The asm8 property returns a numpy.timedelta64 scalar, which represents time duration in nanoseconds internally. This is useful when you need to work with the underlying numpy representation. Example Following is the code − import pandas as pd # TimeDeltas is Python's standard datetime library uses a different ...

Read More

Python Pandas - Check if the Pandas Index holds Interval objects

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 278 Views

To check if a Pandas Index holds Interval objects, use the is_interval() method. This method returns True if the index contains interval objects, False otherwise. What are Interval Objects? Pandas Interval objects represent bounded intervals between two values. They are useful for categorizing continuous data or representing ranges. import pandas as pd # Create individual Interval objects interval1 = pd.Interval(10, 30) interval2 = pd.Interval(30, 50) print("Interval1:", interval1) print("Interval2:", interval2) Interval1: (10, 30] Interval2: (30, 50] Creating an IntervalIndex You can create a Pandas Index from Interval objects, which ...

Read More

Python - Get the weekday from Timestamp object in Pandas

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 4K+ Views

To get the weekday from a Timestamp object in Pandas, use the timestamp.weekday() method. This returns an integer where Monday = 0, Tuesday = 1, ..., Sunday = 6. Syntax timestamp.weekday() Basic Example Let's create a Timestamp and get its weekday ? import pandas as pd import datetime # Create a Timestamp object timestamp = pd.Timestamp(datetime.datetime(2021, 5, 12)) # Display the Timestamp print("Timestamp:", timestamp) # Get the weekday (0=Monday, 1=Tuesday, ..., 6=Sunday) weekday = timestamp.weekday() print("Weekday number:", weekday) print("This is a Wednesday (2)") Timestamp: 2021-05-12 00:00:00 ...

Read More

Python Pandas - Get the UTC Offset Time

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 2K+ Views

To get the UTC offset time in Pandas, use the timestamp.utcoffset() method. The UTC offset represents the time difference between a timezone and Coordinated Universal Time (UTC). What is UTC Offset? UTC offset shows how many hours a timezone is ahead or behind UTC. For example, EST is UTC-5, while IST is UTC+5:30. Basic Usage First, let's create a timestamp and get its UTC offset ? import pandas as pd # Creating a timestamp with UTC timezone timestamp = pd.Timestamp('2021-10-16T15:12:34.261811624', tz='UTC') print("Timestamp:", timestamp) # Get the UTC offset time offset = timestamp.utcoffset() ...

Read More

Python - Check if the Pandas Index holds categorical data

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 1K+ Views

In Pandas, you can check if an Index contains categorical data using the is_categorical() method. This method returns True if the Index holds categorical data, otherwise False. Syntax index.is_categorical() Creating a Categorical Index First, let's create a Pandas Index with categorical data using the astype() method − import pandas as pd # Creating Pandas index with categorical data index = pd.Index(["Electronics", "Accessories", "Furniture"]).astype("category") # Display the Pandas index print("Pandas Index...") print(index) # Check the data type print("The dtype:") print(index.dtype) # Check whether index holds categorical data print("Does Index ...

Read More

Python Pandas - Construct a naive UTC datetime from a POSIX timestamp

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 535 Views

To construct a naive UTC datetime from a POSIX timestamp, use the pd.Timestamp.utcfromtimestamp() method. A POSIX timestamp represents the number of seconds since January 1, 1970, 00:00:00 UTC. What is a POSIX Timestamp? A POSIX timestamp (also called Unix timestamp) is the number of seconds that have elapsed since the Unix epoch (January 1, 1970, 00:00:00 UTC). It's a standard way to represent time across different systems. Basic Syntax pd.Timestamp.utcfromtimestamp(posix_timestamp) Example Let's create a Pandas timestamp and use it to construct a naive UTC datetime from a POSIX timestamp ? ...

Read More

Python - Check if the Pandas Index only consists of booleans

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 151 Views

To check if a Pandas Index only consists of boolean values, use the is_boolean() method. This method returns True if all index values are boolean (True or False), otherwise False. Syntax index.is_boolean() Return Value Returns True if the index contains only boolean values, False otherwise. Example with Boolean Index Let's create an index with only boolean values and check using is_boolean() ? import pandas as pd # Creating Pandas index with boolean values index = pd.Index([True, True, False, False, True, True, True]) # Display the Pandas index print("Pandas ...

Read More
Showing 3071–3080 of 8,546 articles
« Prev 1 306 307 308 309 310 855 Next »
Advertisements