pandas
Timestamp对象
构造方法
利用字符串格式时间数据转化
pandas支持智能的将各种格式的时间数据转化为标准时间戳格式
1 2 3
| tm1 = pd.Timestamp.now() tm1
|
Timestamp('2022-11-22 18:04:34.850491')
1 2
| tm2 = pd.Timestamp('2022-11-21 16:12:12') tm2
|
Timestamp('2022-11-21 16:12:12')
1 2
| tm3 = pd.Timestamp('2022/11/21 16:12:12') tm3
|
Timestamp('2022-11-21 16:12:12')
1 2 3
| tm4 = pd.to_datetime(time.ctime()) tm4
|
Timestamp('2022-11-22 18:04:59')
利用时间戳进行转换
1
| pd.Timestamp(1669112020.1238306, unit='s')
|
Timestamp('2022-11-22 10:13:40.123830557')
1 2
| d = pd.Timestamp(1669112020.1238306, unit='s', tz=tz1) d
|
Timestamp('2022-11-22 18:13:40.123830557+0800', tz='新时区')
通过直接指定相应时间属性值
1 2
| pd.Timestamp(year=2022, month=11, day=21, hour=20, minute=28, second=30, microsecond=30)
|
Timestamp('2022-11-21 20:28:30.000030')
1 2
| d2 = pd.Timestamp(2022, 11, 30, 20, 26,30) d2
|
Timestamp('2022-11-30 20:26:30')
属性
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| print(d.tz) print(d.tzinfo)
print(d.asm8)
print(d.weekofyear)
print(d.day_of_week)
print(d.day_of_year)
print(d.days_in_month) print(d.daysinmonth)
print(d.year) print(d.month) print(d.day) print(d.hour) print(d.minute) print(d.second) print(d.nanosecond) print(d.microsecond) print(d.quarter)
print(d.is_leap_year) print(d2.is_month_end) print(d.is_month_start) print(d.is_year_end) print(d.is_year_start)
|
新时区
新时区
2022-11-22T10:13:40.123830557
47
1
326
30
30
2022
11
22
18
13
40
557
123830
4
False
True
False
False
False
方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| print(d.ctime())
print(d.day_name())
print(d.timestamp())
print(d.fromtimestamp(d.timestamp()))
print(d.utcfromtimestamp(d.timestamp()))
print(d.toordinal() )
print(d.fromordinal(d.toordinal()))
print(d.isocalendar())
print(d.timetuple())
print(d.tzname())
print(d.isoformat())
|
Tue Nov 22 18:13:40 2022
Tuesday
1669112020.123831
2022-11-22 18:13:40.123831
2022-11-22 10:13:40.123831
738481
2022-11-22 00:00:00
(2022, 47, 2)
time.struct_time(tm_year=2022, tm_mon=11, tm_mday=22, tm_hour=18, tm_min=13, tm_sec=40, tm_wday=1, tm_yday=326, tm_isdst=-1)
新时区
2022-11-22T18:13:40.123830557+08:00
Timedelta时间段
构造方法
通过字符串格式进行定义
1 2
| pd.Timedelta('2days 2hours 15minutes 30seconds')
|
Timedelta('2 days 02:15:30')
1 2
| pd.Timedelta('2d2h15min30s')
|
Timedelta('2 days 02:15:30')
通过整数值与指定单位定义
1
| pd.Timedelta(150, unit='s')
|
Timedelta('0 days 00:02:30')
1
| pd.Timedelta(1.5, unit='d')
|
Timedelta('1 days 12:00:00')
直接指定相应属性定义
1
| pd.Timedelta(days=2, hours=15, minutes=30, seconds=30)
|
Timedelta('2 days 15:30:30')
1 2 3 4
| print(pd.to_timedelta('2d')) print(pd.to_timedelta('2d3m')) print(pd.to_timedelta(200, unit='s'))
|
2 days 00:00:00
2 days 00:03:00
0 days 00:03:20
date_range时间区间
其中每个元素都为Timestamp对象
指定起点、终点和数据量
1
| pd.date_range(start='2022-11-1', end='2022-11-30', periods=30)
|
DatetimeIndex(['2022-11-01', '2022-11-02', '2022-11-03', '2022-11-04',
'2022-11-05', '2022-11-06', '2022-11-07', '2022-11-08',
'2022-11-09', '2022-11-10', '2022-11-11', '2022-11-12',
'2022-11-13', '2022-11-14', '2022-11-15', '2022-11-16',
'2022-11-17', '2022-11-18', '2022-11-19', '2022-11-20',
'2022-11-21', '2022-11-22', '2022-11-23', '2022-11-24',
'2022-11-25', '2022-11-26', '2022-11-27', '2022-11-28',
'2022-11-29', '2022-11-30'],
dtype='datetime64[ns]', freq=None)
指定起点、终点和步长
1
| pd.date_range(start='2022-11-1', end='2022-11-30', freq='3d')
|
DatetimeIndex(['2022-11-01', '2022-11-04', '2022-11-07', '2022-11-10',
'2022-11-13', '2022-11-16', '2022-11-19', '2022-11-22',
'2022-11-25', '2022-11-28'],
dtype='datetime64[ns]', freq='3D')
指定起点、数据量和步长
1
| pd.date_range(start='2022-11-3', periods=10, freq='2d')
|
DatetimeIndex(['2022-11-03', '2022-11-05', '2022-11-07', '2022-11-09',
'2022-11-11', '2022-11-13', '2022-11-15', '2022-11-17',
'2022-11-19', '2022-11-21'],
dtype='datetime64[ns]', freq='2D')
指定终点、数据量和步长
1
| pd.date_range(end='2022-11-3 9:00:00', periods=10, freq='3d')
|
DatetimeIndex(['2022-10-07 09:00:00', '2022-10-10 09:00:00',
'2022-10-13 09:00:00', '2022-10-16 09:00:00',
'2022-10-19 09:00:00', '2022-10-22 09:00:00',
'2022-10-25 09:00:00', '2022-10-28 09:00:00',
'2022-10-31 09:00:00', '2022-11-03 09:00:00'],
dtype='datetime64[ns]', freq='3D')