📜  Python| Pandas TimedeltaIndex.shape(1)

📅  最后修改于: 2023-12-03 15:34:16.549000             🧑  作者: Mango

Python | Pandas TimedeltaIndex.shape

Pandas is a powerful data manipulation library for Python. It provides easy-to-use data structures and data analysis tools. In this tutorial, we will discuss the TimedeltaIndex.shape attribute in Pandas.

Introduction

A TimedeltaIndex is a special type of index in Pandas that contains time durations as its elements. The TimedeltaIndex is created by passing a list of timedeltas to the constructor of the TimedeltaIndex class.

The TimedeltaIndex.shape attribute returns a tuple that contains the number of rows and columns in the TimedeltaIndex.

Syntax

The syntax for using the TimedeltaIndex.shape attribute in Pandas is as follows:

timedelta_index.shape
Parameters

The TimedeltaIndex.shape attribute does not take any parameters.

Return Value

The TimedeltaIndex.shape attribute returns a tuple that contains the number of rows and columns in the TimedeltaIndex.

Example

In this example, we will create a TimedeltaIndex and use the TimedeltaIndex.shape attribute to get the number of rows and columns in the index:

import pandas as pd
import numpy as np

# create a TimedeltaIndex
timedeltas = [pd.Timedelta(days=i) for i in range(5)]
t_index = pd.TimedeltaIndex(timedeltas)
print(t_index)

# get the shape of the TimedeltaIndex
shape = t_index.shape
print(shape)

Output:

TimedeltaIndex(['0 days', '1 days', '2 days', '3 days', '4 days'], dtype='timedelta64[ns]', freq=None)
(5,)

The output shows that the TimedeltaIndex contains 5 rows and 1 column. The TimedeltaIndex.shape attribute returns a tuple with only one element since the index has only one dimension.

Conclusion

In this tutorial, we discussed the TimedeltaIndex.shape attribute in Pandas. The TimedeltaIndex is a specialized index that contains time durations as its elements. The TimedeltaIndex.shape attribute returns a tuple that contains the number of rows and columns in the index.