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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
