Timestamp 是 Python 的 Datetime 的 Pandas 實現,它們的函數和屬性幾乎相同。 Timestamp 是用於 DatetimeIndex 的底層數據結構。
參數
1.ts_input | datetime-like 或 string 或 int 或 numeric
用於創建 Timestamp 的值。
2. freq | string 或 DateOffset | optional
Timestamp 的間隔。
3. tz | string 或 pytz.timezone 或 dateutil.tz.tzfile | optional
Timestamp 的時區。
4. unit | string | optional
時間戳使用的單位:
"D", "h", "m", "s", "ms", "us" and "ns"
5. year | int | optional
年單位。
6. month | int | optional
月份單位。
7. day | int | optional
日單位。
注意
您可以通過兩種不同的方式初始化Timestamp:
-
指定
ts_input,其中包含有關年、月和日的信息(例如"2020-01-15")。 -
指定
year、month和day。
8. hour | int | optional
默認情況下,hour=0 。
9. minute | int | optional
默認情況下,minute=0 。
10.second | int | optional
默認情況下,second=0 。
11.microsecond | int | optional
默認情況下,microsecond=0 。
返回值
Timestamp 對象。
例子
基本用法
要從日期字符串創建 Timestamp 對象:
pd.Timestamp("2020-01-25")
Timestamp('2020-01-25 00:00:00')
要創建帶有時間單位的Timestamp:
pd.Timestamp("2020-01-25 22:15:25")
Timestamp('2020-01-25 22:15:25')
指定頻率
創建頻率為一天的Timestamp:
pd.Timestamp("2020-01-25", freq="D")
Timestamp('2020-01-25 00:00:00', freq='D')
參數freq很少用於單個時間戳。它們通常用於創建時間戳序列的方法,例如 date_range(~) 。
指定 tz
要創建時區感知的 Timestamp:
pd.Timestamp("2020-01-25", tz="Asia/Tokyo")
Timestamp('2020-01-25 00:00:00+0900', tz='Asia/Tokyo')
指定單位
使用 UNIX 時間戳創建 Timestamp:
pd.Timestamp(1608854400, unit="s")
Timestamp('2020-12-25 00:00:00')
這裏,1608854400 表示自 1970 年 1 月 1 日(UTC)以來經過的秒數。
指定年、月、日
要使用單獨的時間參數創建Timestamp:
pd.Timestamp(year=2020, month=1, day=25)
Timestamp('2020-01-25 00:00:00')
請注意,您必須指定所有這三個參數,否則將引發錯誤。
相關用法
- Python Pandas Timedelta構造函數用法及代碼示例
- Python Timer cancel()用法及代碼示例
- Python Timer start()用法及代碼示例
- Python BeautifulSoup Tag contents屬性用法及代碼示例
- Python BeautifulSoup Tag string屬性用法及代碼示例
- Python Thread join()用法及代碼示例
- Python Django TodayArchiveView用法及代碼示例
- Python Tensorflow asin()用法及代碼示例
- Python Django TransactionNow用法及代碼示例
- Python Thread run()用法及代碼示例
- Python TextBlob.correct()用法及代碼示例
- Python Tuple len()用法及代碼示例
- Python Tensorflow math.accumulate_n()用法及代碼示例
- Python Tensorflow cosh()用法及代碼示例
- Python Django TransactionTestCase.reset_sequences用法及代碼示例
- Python BeautifulSoup Tag decompose方法用法及代碼示例
- Python Tuple min()用法及代碼示例
- Python Thread setName()用法及代碼示例
- Python TextCalendar prmonth()用法及代碼示例
- Python Tensorflow sin()用法及代碼示例
- Python Tuples轉Dictionary用法及代碼示例
- Python Django TestCase.setUpTestData用法及代碼示例
- Python Tensorflow acos()用法及代碼示例
- Python Tuple轉integer用法及代碼示例
- Python Django TransactionTestCase.assertNumQueries用法及代碼示例
注:本文由純淨天空篩選整理自Isshin Inada大神的英文原創作品 Pandas | Timestamp constructor。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
