Found 26504 Articles for Server Side Programming

Python - Check if the Pandas Index is of the object dtype

AmitDiwan
Updated on 13-Oct-2021 07:38:02

687 Views

To check if the Pandas Index is of the object dtype, use the index.is_object() method. At first, import the required libraries -import pandas as pdCreating Pandas index −index = pd.Index(["Electronics", 6, 10.5, "Accessories", 25.6, 30]) Display the Pandas index −print("Pandas Index...", index)Check whether index values has object dtype −print("Is the Index of object dtype?", index.is_object()) ExampleFollowing is the code −import pandas as pd # Creating Pandas index index = pd.Index(["Electronics", 6, 10.5, "Accessories", 25.6, 30]) # Display the Pandas index print("Pandas Index...", index) # Return the number of elements in the Index print("Number of elements in the ... Read More

Python Pandas - Get the timedelta in nanoseconds for internal compatibility

Arnab Chakraborty
Updated on 13-Oct-2021 07:37:24

911 Views

Use the timedelta.delta property in Pandas to get the timedelta in nanoseconds for internal compatibility.At first, import the required libraries −import pandas as pdTimeDeltas is Python’s standard datetime library uses a different representation timedelta’s. Create a Timedelta objecttimedelta = pd.Timedelta('5 days 1 min 45 s 40 ns') Return the timedelta in nanosecondstimedelta.deltaExampleFollowing is the code import pandas as pd # TimeDeltas is Python’s standard datetime library uses a different representation timedelta’s # create a Timedelta object timedelta = pd.Timedelta('5 days 1 min 45 s 40 ns') # display the Timedelta print("Timedelta...", timedelta) # return the timedelta in ... Read More

Python - Check if the Pandas Index only consists of numeric data

AmitDiwan
Updated on 13-Oct-2021 07:32:57

611 Views

To check if the Pandas Index only consists of numeric data, use the index.is_numeric() method. At first, import the required libraries -import pandas as pd import numpy as npCreating Pandas index with integer, float and NaNsindex = pd.Index([5, 10.2, 25, 50, 75.2, 100, np.nan]) Display the Pandas index −print("Pandas Index...", index)Check whether index values has only numeric data. Numeric data includes integer, floats and NaNs −index.is_numeric() ExampleFollowing is the code −import pandas as pd import numpy as np # Creating Pandas index with integer, float and NaNs index = pd.Index([5, 10.2, 25, 50, 75.2, 100, np.nan]) # Display ... Read More

Python Pandas - Get the number of days from TimeDelta

Arnab Chakraborty
Updated on 13-Oct-2021 07:32:57

9K+ Views

To get the number of days from TimeDelta, use the timedelta.days property in Pandas. At first, import the required libraries −import pandas as pdTimeDeltas is Python’s standard datetime library uses a different representation timedelta’s. Create a Timedelta objecttimedelta = pd.Timedelta('5 days 1 min 45 s') Return the number of daystimedelta.daysExampleFollowing is the code import pandas as pd # TimeDeltas is Python’s standard datetime library uses a different representation timedelta’s # create a Timedelta object timedelta = pd.Timedelta('5 days 1 min 45 s') # display the Timedelta print("Timedelta...", timedelta) # return the number of days res = timedelta.days ... Read More

Python Pandas - Return a components namedtuple-like

Arnab Chakraborty
Updated on 13-Oct-2021 07:31:07

162 Views

To return a components namedtuple-like, use the timedelta.components. At first, import the required libraries −import pandas as pdTimeDeltas is Python’s standard datetime library uses a different representation timedelta’s. Create a Timedelta objecttimedelta = pd.Timedelta('1 days 10 min 20 s') Return a components namedtuple-liketimedelta.componentsExampleFollowing is the code import pandas as pd # TimeDeltas is Python’s standard datetime library uses a different representation timedelta’s # create a Timedelta object timedelta = pd.Timedelta('1 days 10 min 20 s') # display the Timedelta print("Timedelta...", timedelta) # return a components namedtuple-like res = timedelta.components # display the component print("Component...", res)OutputThis will ... Read More

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

AmitDiwan
Updated on 13-Oct-2021 07:30:37

151 Views

To check if the Pandas Index with some NaNs is a floating type, use the index.is_floating() method. At first, import the required libraries −import pandas as pd import numpy as npCreating Pandas index with some NaNs −index = pd.Index([5.7, 6.8, 10.5, np.nan, 17.8, 25.6, np.nan ,np.nan, 50.2]) Display the Pandas index −print("Pandas Index...", index)Check whether index values with some NaNs are floating type −print("Index values with some NaNs is a floating type?", index.is_floating())ExampleFollowing is the code −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, 25.6, ... Read More

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

Arnab Chakraborty
Updated on 13-Oct-2021 07:28:36

274 Views

To return a numpy timedelta64 array scalar view in nanoseconds, use the timedelta.asm8 property in Pandas.At first, import the required libraries −import pandas as pdExampleFollowing is the code import pandas as pd # TimeDeltas is Python’s standard datetime library uses a different representation timedelta’s # create a Timedelta object timedelta = pd.Timedelta('10 min 20 s') # display the Timedelta print("Timedelta...", timedelta) # getting the timedelta64 in nanoseconds res = timedelta.asm8 # display the timedelta64 print("Timedelta64 array scalar view...", res)OutputThis will produce the following code Timedelta...  0 days 00:10:20 Timedelta64 array scalar view... 620000000000 nanoseconds

Python Pandas - Check if the Pandas Index holds Interval objects

AmitDiwan
Updated on 13-Oct-2021 07:27:11

233 Views

To check if the Pandas Index holds Interval objects, use the index.is_interval() method in Pandas.At first, import the required libraries -import pandas as pdCreate Interval objects −interval1 = pd.Interval(10, 30) interval2 = pd.Interval(30, 50)Display the intervals −print("Interval1...", interval1) print("Interval2...", interval2)Creating Pandas index with Interval object1 and 2 −index = pd.Index([interval1, interval2]) Check whether index values has only interval objects −print("Does Index consists of Interval objects?", index.is_interval())ExampleFollowing is the code −import pandas as pd # create Interval objects interval1 = pd.Interval(10, 30) interval2 = pd.Interval(30, 50) # display the intervals print("Interval1...", interval1) print("Interval2...", interval2) # Creating Pandas index ... Read More

Python - Get the weekday from Timestamp object in Pandas

Arnab Chakraborty
Updated on 13-Oct-2021 07:26:25

4K+ Views

To get the weekday from Timestamp object, use the timestamp.weekday() method. At first, import the required libraries −import pandas as pd import datetimeSet the timestamp in Pandas. Create a Timestamp objecttimestamp = pd.Timestamp(datetime.datetime(2021, 5, 12)) Get the weekday of the year. Weekday is represented by number Monday == 0, Tuesday == 1 … Sunday == 6timestamp.weekday()ExampleFollowing is the code import pandas as pd import datetime # set the timestamp in Pandas # create a Timestamp object timestamp = pd.Timestamp(datetime.datetime(2021, 5, 12)) # display the Timestamp print("Timestamp...", timestamp) # getting the weekday of the year res = timestamp.weekday() ... Read More

Python Pandas - Get the UTC Offset Time

Arnab Chakraborty
Updated on 13-Oct-2021 07:23:44

2K+ Views

To get the UTC Offset Time, use the timestamp.utcoffset(). At first, import the required libraries −import pandas as pdCreating a timestamptimestamp = pd.Timestamp('2021-10-16T15:12:34.261811624', tz='UTC') New timestamp with UTC day and timetimestamp.utcnow()Get the UTC offset timetimestamp.utcoffset() ExampleFollowing is the code import pandas as pd # creating a timestamp timestamp = pd.Timestamp('2021-10-16T15:12:34.261811624', tz='UTC') # display the Timestamp print("Timestamp...", timestamp) # new timestamp with UTC day and time print("UTC day and time...", timestamp.utcnow()) # Get the UTC offset time print("UTC offset time...", timestamp.utcoffset())OutputThis will produce the following code Timestamp...  2021-10-16 15:12:34.261811624+00:00 UTC day and time... 2021-10-03 07:56:44.685816+00:00 ... Read More

Advertisements