python timedelta对象_Python Pandas TimedeltaIndex.take()用法及代码示例

Python是进行数据分析的一种出色语言,主要是因为以数据为中心的python软件包具有奇妙的生态系统。 Pandas是其中的一种,使导入和分析数据更加容易。

Pandas TimedeltaIndex.take()函数返回由索引选择的值的新索引。我们通常会传递要采用的索引列表。它用于与numpy数组进行内部兼容。

用法: TimedeltaIndex.take(indices, axis=0, allow_fill=True, fill_value=None, **kwargs)

参数:

indices:列出要采取的指数

axis:选择值的轴,始终为0。

allow_fill:布尔值,默认为True

fill_value:bool,默认为None,如果allow_fill = True且fill_value不为None,则将-1指定的索引视为NA。如果Index不包含NA,则引发ValueError

返回:相同类型的对象

范例1:采用TimedeltaIndex.take()函数返回一个新的TimedeltaIndex对象,该对象中仅包含选定的值。

# importing pandas as pd

import pandas as pd

# Create the TimedeltaIndex object

tidx = pd.TimedeltaIndex(data =['06:05:01.000030', '+23:59:59.999999',

'22 day 2 min 3us 10ns', '+23:29:59.999999',

'+12:19:59.999999'])

# Print the TimedeltaIndex object

print(tidx)

输出:

ad092e5a5b5ebdb1505d9a528905192f.png

现在我们将使用TimedeltaIndex.take()函数从tidx中选择一些特定值。

# select specific values.

tidx.take([2, 3, 4])

输出:

0430dcb154ff6a5137e39e89ab2a324e.png

正如我们在输出中看到的,TimedeltaIndex.take()函数返回了一个新的TimedeltaIndex对象,该对象仅包含其位置已传递给该函数的那些元素。

范例2:采用TimedeltaIndex.take()函数返回一个新的TimedeltaIndex对象,该对象中仅包含选定的值。

# importing pandas as pd

import pandas as pd

# Create the TimedeltaIndex object

tidx = pd.TimedeltaIndex(start ='1 days 02:00:12.001124',

periods = 5, freq ='D', name ='Koala')

# Print the TimedeltaIndex object

tidx

输出:

0752bbefd9df4df9773d627f384d1026.png

现在我们将使用TimedeltaIndex.take()函数从tidx中选择一些特定值。

# select specific values.

tidx.take([0, 1, 2])

输出:

7706a2269b970f038dd458b000e800f6.png

正如我们在输出中看到的,TimedeltaIndex.take()函数返回了一个新的TimedeltaIndex对象,该对象仅包含其位置已传递给该函数的那些元素。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值