Pandas DatetimeIndex 构造函数创建一个新的 DatetimeIndex 对象,该对象最常用作 DataFrame 的索引。
警告
选择使用 pd.date_range(~) 方法来初始化DatetimeIndex,而不是直接使用此构造函数。 date_range(~) 提供了更大的灵活性,并且在官方文档中使用更广泛。
参数
1.data | 1D array-like | optional
用于构造 DatetimeIndex 的类似数组。
2. freq | string | optional
DatetimeIndex 的频率。默认情况下,freq=None 。
3. tz | pytz.timezone 或 dateutil.tz.tzfile 或 datetime.tzinfo 或 string | optional
DatetimeIndex 的时区。默认情况下,生成的 DatetimeIndex 很幼稚,因为它没有时区的概念。
4. normalize | boolean | optional
是否将日期的时间单位设置为午夜。默认情况下,normalize=False 。
警告
设置这个参数好像没有什么效果。如果您遇到同样的问题,请改用 date_range(~) 的 normalize。
5. closed | None 或 string | optional
是否使边界包含/排除:
|
值 |
说明 |
|---|---|
|
|
|
|
|
|
|
|
两个界限都变得具有包容性。 |
默认情况下,closed=None 。
6. ambiguous | string 或 array<boolean> | optional
仅当您指定时区 (tz) 时,此参数才相关。由于夏令时 (DST) 引起的时间调整,时间可能会出现歧义。例如,考虑以下情况:
Local time:
01:59:58
01:59:59
01:00:00 # DST ends and so we set the wall clock back 1 hour
01:00:01
...
01:59:58 # This local time occured for the second time
...
如果您尝试本地化发生两次的时间(例如 01:59:58 ),那么 Pandas 会很困惑您指的是哪个时间 - 第一次(DST)还是第二次(非 DST)?
Pandas 可以通过以下方式之一处理这种歧义:
|
值 |
说明 |
|---|---|
|
|
根据提供的时间顺序推断 DST 转换。 |
|
|
布尔数组(例如列表、Numpy 数组),其中:
|
|
|
不明确的时间将转换为 |
|
|
模棱两可的时候会引发错误。 |
默认情况下,ambiguous="raise" 。
7. dayfirst | boolean | optional
如果 True ,则将第一个数字视为一天。例如, "10/12/2020" 将被解析为 2020 年 12 月 10 日。默认情况下, dayfirst=False 。
8. yearfirst | boolean | optional
如果 True ,则将第一个数字视为年份。例如, "20/12/10" 将被解析为 2020 年 12 月 10 日。默认情况下, yearfirst=False 。
9. dtype | string 或 numpy.dtype 或 DatetimeTZDtype | optional
允许的 dtype 是 'datetime64[ns]' 。您可以使用 dtype 嵌入时区,而不是指定 tz ,如下所示:
dtype="datetime64[ns, Europe/Paris]"
10.copy | boolean | optional
-
如果
True,则将返回data的新副本 - 修改此复制的数据不会影响原始数据,反之亦然。 -
如果是
False,则将返回对data的引用 - 修改返回值将改变原始数据,反之亦然。
默认情况下,copy=False 。
11.name | string | optional
分配给生成的 DatetimeIndex 的名称。默认情况下,name=None 。
返回值
DatetimeIndex 对象。
例子
基本用法
要使用日期字符串列表创建DatetimeIndex:
idx = pd.DatetimeIndex(["2020-12-25","2020-12-26"])
idx
DatetimeIndex(['2020-12-25', '2020-12-26'], dtype='datetime64[ns]', freq=None)
我们可以用它来初始化 DataFrame 和 DatetimeIndex :
df = pd.DataFrame({"A":["a","b"]}, index=idx)
df
A
2020-12-25 a
2020-12-26 b
指定时区
要指定时区,请设置tz,如下所示:
pd.DatetimeIndex(["2020-12-25","2020-12-26"], tz="Asia/Tokyo")
DatetimeIndex(['2020-12-25 00:00:00+09:00', '2020-12-26 00:00:00+09:00'], dtype='datetime64[ns, Asia/Tokyo]', freq=None)
这里,附加的+09:00表示东京的标准时间比UTC早9个小时。
处理暧昧时期
2019-10-27 3AM(中欧时间),DST 结束,这意味着挂钟向后拨回一小时。因此,我们这里有一个不明确的情况,其中像 2019-10-27 02:30:00 这样的本地时间出现了两次。
增加
默认情况下, ambiguous="raise" ,这意味着如果日期不明确,则会引发错误:
pd.DatetimeIndex(['2019-10-27 02:30:00',
'2019-10-27 02:00:00',
'2019-10-27 02:30:00',
'2019-10-27 03:00:00',
'2019-10-27 03:30:00'], tz="CET")
AmbiguousTimeError: Cannot infer dst time from 2019-10-27 02:30:00, try using the 'ambiguous' argument
推断
设置 ambiguous="infer" 告诉 Pandas 尝试解决提供的日期序列中的歧义:
pd.DatetimeIndex(['2019-10-27 02:30:00',
'2019-10-27 02:00:00',
'2019-10-27 02:30:00',
'2019-10-27 03:00:00',
'2019-10-27 03:30:00'], tz="CET", ambiguous="infer")
DatetimeIndex(['2019-10-27 02:30:00+02:00', '2019-10-27 02:00:00+01:00',
'2019-10-27 02:30:00+01:00', '2019-10-27 03:00:00+01:00',
'2019-10-27 03:30:00+01:00'],
dtype='datetime64[ns, CET]', freq=None)
在这里,请注意 Pandas 如何推断第一个 02:30:00 处于夏令时,而第二个 02:30:30 处于非夏令时。
布尔数组
要明确告诉 Pandas 日期是否应该解析为 DST 还是非 DST,请传递一个布尔数组,其中 True 表示 DST:
pd.DatetimeIndex(['2019-10-27 02:30:00',
'2019-10-27 02:50:00'], tz="CET", ambiguous=[True, False])
DatetimeIndex(['2019-10-27 02:30:00+02:00', '2019-10-27 02:50:00+01:00'], dtype='datetime64[ns, CET]', freq=None)
NaT
要将不明确的日期设置为NaT (not-a-time):
pd.DatetimeIndex(['2019-10-27 02:30:00',
'2019-10-27 02:00:00',
'2019-10-27 02:30:00',
'2019-10-27 03:00:00',
'2019-10-27 03:30:00'], tz="CET", ambiguous="NaT")
DatetimeIndex(['NaT', 'NaT', 'NaT', '2019-10-27 03:00:00+01:00',
'2019-10-27 03:30:00+01:00'],
dtype='datetime64[ns, CET]', freq=None)
相关用法
- Python Datetime.replace()用法及代码示例
- Python Datetime today方法用法及代码示例
- Python Datetime utcnow方法用法及代码示例
- Python Datetime fromisoformat方法用法及代码示例
- Python Datetime Date构造函数用法及代码示例
- Python Datetime strftime方法用法及代码示例
- Python Datetime Time构造函数用法及代码示例
- Python Datetime strptime方法用法及代码示例
- Python Datetime Timedelta构造函数用法及代码示例
- Python Datetime utcfromtimestamp方法用法及代码示例
- Python Datetime now方法用法及代码示例
- Python Datetime fromtimestamp方法用法及代码示例
- Python Datetime Datetime构造函数用法及代码示例
- Python Django DateDetailView用法及代码示例
- Python DateTime转integer用法及代码示例
- Python DateTime astimezone()用法及代码示例
- Python Django DateTimeField.input_formats用法及代码示例
- Python DateTime weekday()用法及代码示例
- Python Pandas DataFrame empty属性用法及代码示例
- Python Pandas DataFrame pop方法用法及代码示例
- Python Pandas DataFrame nsmallest方法用法及代码示例
- Python Django DataSource用法及代码示例
- Python Pandas DataFrame sample方法用法及代码示例
- Python Pandas DataFrame items方法用法及代码示例
- Python Pandas DataFrame max方法用法及代码示例
注:本文由纯净天空筛选整理自Isshin Inada大神的英文原创作品 Pandas | DatetimeIndex constructor。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
