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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。