Pandas to_datetime(~) 方法将参数转换为 datetime 对象。
参数
1.arg | number 或 string 或 datetime 或 sequence 或 map
要转换为 datetime 对象的对象。
2. errors | string | optional
转换不成功的处理方法:
|
值 |
说明 |
|---|---|
|
|
提出错误。 |
|
|
|
|
|
返回输入。 |
默认情况下,errors="raise" 。
3. dayfirst | boolean | optional
如果 True ,则将第一个数字视为一天。例如, "10/12/2020" 将被解析为 2020 年 12 月 10 日。默认情况下, dayfirst=False 。
4. yearfirst | boolean | optional
如果 True ,则将第一个数字视为年份。例如, "20/12/10" 将被解析为 2020 年 12 月 10 日。默认情况下, yearfirst=False 。
5. utc | boolean | optional
是否将时区设置为 UTC。默认情况下,utc=False 。
6. format | string | optional
日期的格式字符串,遵循标准 Python 语法(例如 "%d/%m/%Y" )。
7. exact | boolean | optional
是否对指定的 format 强制执行精确匹配。默认情况下,exact=True 。
8. unit | string | optional
参数的时间单位:
"D", "s", "ms", "us", "ns"
默认情况下,unit="ns" 。
9. infer_datetime_format | boolean | optional
如果未指定 format 且此参数设置为 True ,则在可能的情况下推断格式。如果可以推断格式,则可以更有效地解析日期。
默认情况下,infer_datetime_format=False 。
10.origin | scalar | optional
使用的参考日期:
-
"unix":使用 1970-01-01 作为参考日期 -
"julian":使用儒略历的开始作为参考日期
默认情况下,origin="unix" 。
11.cache | boolean | optional
解析日期时是否利用缓存。使用缓存将加快解析重复日期的过程,特别是那些具有时区偏移量的日期。请注意,只有当要解析的日期数量至少为 50 时,缓存才会生效。默认为 cache=False 。
返回值
返回类型取决于 arg 的类型:
-
array-like:返回DatetimeIndex。 -
Series:返回datetime64类型的Series。 -
scalar:返回Timestamp。
例子
时间戳
要将 MM/DD/YYYY 格式的日期字符串转换为 Timestamp :
pd.to_datetime("10/12/2020") # October
Timestamp('2020-10-12 00:00:00')
请注意 Pandas 如何正式使用 YYYY-MM-DD 来表示日期。
日期字符串也可以采用 YYYY/MM/DD 格式:
pd.to_datetime("2020/12/10") # December
Timestamp('2020-12-10 00:00:00')
很容易混淆日期和月份,因此一个好的做法是指定 format 参数:
pd.to_datetime("10/12/2020", format="%d/%m/%Y")
Timestamp('2020-12-10 00:00:00')
日期时间64
要将一系列日期字符串转换为一系列 dtype datetime64[ns] :
pd.to_datetime(pd.Series(["25/12/2020", "26/12/2020"]))
0 2020-12-25
1 2020-12-26
dtype: datetime64[ns]
DatetimeIndex
要将日期字符串数组转换为 DatetimeIndex (可用作 DataFrame 的索引),请传入一个数组,如下所示:
pd.to_datetime(["25/12/2020", "26/12/2020"])
DatetimeIndex(['2020-12-25', '2020-12-26'], dtype='datetime64[ns]', freq=None)
相关用法
- Python Pandas to_timedelta方法用法及代码示例
- Python Pandas to_numeric方法用法及代码示例
- Python torch.distributed.rpc.rpc_async用法及代码示例
- Python torch.nn.InstanceNorm3d用法及代码示例
- Python torchaudio.transforms.Fade用法及代码示例
- Python torch.special.gammaincc用法及代码示例
- Python torch.optim.lr_scheduler.ConstantLR用法及代码示例
- Python torch.normal用法及代码示例
- Python torchdata.datapipes.iter.Multiplexer用法及代码示例
- Python torch.nn.quantized.dynamic.LSTM用法及代码示例
- Python torch.nn.EmbeddingBag用法及代码示例
- Python torch.nn.Module.register_forward_hook用法及代码示例
- Python torch.nn.AvgPool2d用法及代码示例
- Python torch.nn.PixelShuffle用法及代码示例
- Python torch.Generator.initial_seed用法及代码示例
- Python torch.resolve_neg用法及代码示例
- Python torchtext.vocab.Vectors.get_vecs_by_tokens用法及代码示例
- Python torch.nn.CELU用法及代码示例
- Python torch.reciprocal用法及代码示例
- Python torch.nn.Hardsigmoid用法及代码示例
- Python torch.fft.fft用法及代码示例
- Python torch.distributed.TCPStore用法及代码示例
- Python torch.distributed.pipeline.sync.skip.skippable.stash用法及代码示例
- Python torch.nn.GLU用法及代码示例
- Python torch.nn.functional.conv1d用法及代码示例
注:本文由纯净天空筛选整理自Isshin Inada大神的英文原创作品 Pandas | to_datetime method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
