import pandas as pd
import numpy as np
import time
import datetime
pandas.Timestamp(ts_input=<object object>, freq=None, tz=None, unit=None, year=None, month=None, day=None, hour=None, minute=None, second=None, microsecond=None, nanosecond=None, tzinfo=None, *, fold=None)
yyyy-mm-dd hh:mm:ss
格式。pd.Timestamp('2022-01-01')
#Timestamp('2022-01-01 00:00:00')
pd.Timestamp('2021-12-15 12')
#Timestamp('2021-12-15 12:00:00')
pd.Timestamp('01-01-2022 12')
#Timestamp('2022-01-01 12:00:00')
pd.Timestamp('2022-01')
#Timestamp('2022-01-01 00:00:00')
pd.Timestamp('2022')
#Timestamp('2022-01-01 00:00:00')
ValueError: could not convert string to Timestamp
错误。pd.Timestamp('2022-01-50')
pd.Timestamp('2022-02-31')
pd.Timestamp('2022-02-29')
time.time()
#1650616591.3381748
pd.Timestamp(time.time(), unit="s")
#Timestamp('2022-04-22 08:36:15.079070091')
pd.Timestamp(int(time.time()), unit="s")
#Timestamp('2022-04-22 08:36:56')
my_time = time.time()
print(my_time)
pd.Timestamp(my_time)
#1650616682.428626
#Timestamp('1970-01-01 00:00:01.650616682')
pd.Timestamp(2)
#Timestamp('1970-01-01 00:00:00.000000002')
pd.Timestamp(2022, 1, 10, 12)
#Timestamp('2022-01-10 12:00:00')
pd.Timestamp(1, 2022, 10, 12)
#ValueError: month must be in 1..12
pd.Timestamp(year=2022,day=10, month=1)
#Timestamp('2022-01-10 00:00:00')
pd.Timestamp(year=2022, month=1,day=1)
#Timestamp('2022-01-01 00:00:00')
pd.Timestamp(year=2022, month=1, day=10, hour=12)
#Timestamp('2022-01-10 12:00:00')
pd.Timestamp(year=2022, month=1, day=10)
#Timestamp('2022-01-10 00:00:00')
pd.Timedelta(value=<object object at 0x000001BE55DCFE80>,unit=None,**kwargs)
ts = pd.Timestamp('2022-01-01 12')
ts
#Timestamp('2022-01-01 12:00:00')
ts + pd.Timedelta(-1, "D")
#Timestamp('2021-12-31 12:00:00')
td = pd.Timedelta(days=5, minutes=50, seconds=20)
td
#Timedelta('5 days 00:50:20')
ts + td
Timestamp('2022-01-06 12:50:20')
td.total_seconds()
#435020.0
datetime.datetime.now()
#datetime.datetime(2023, 2, 22, 15, 22, 19, 554438)
pd.Timestamp(int(time.time()) + 435020, unit="s", tz="Asia/Shanghai")
#Timestamp('2022-04-27 17:41:29+0800', tz='Asia/Shanghai')
pd.Timestamp(int(time.time()), unit="s", tz="Asia/Shanghai")
Timestamp('2022-04-22 16:51:25+0800', tz='Asia/Shanghai')
time.time()+ td.total_seconds()
pd.Timestamp(int(time.time()+ td.total_seconds()),unit='s')
#Timestamp('2022-04-27 09:38:11')
import datetime
now = datetime.datetime.now()
print(now)
dt = now + datetime.timedelta(days=100)
print(dt,type(dt))
dt.strftime('%Y-%m-%d')
#2022-04-22 16:54:11.351536
#2022-07-31 16:54:11.351536
#'2022-07-31'
to_datetime
转换时间戳。to_datetime(arg, errors='raise', dayfirst=False, yearfirst=False, utc=None, format=None, unit=None, infer_datetime_format=False, origin='unix')
yearfirst
。df = pd.DataFrame({'year': [2015, 2016],'month': [2, 3],'day': [4, 5]})
df
#year month day
#0 2015 2 4
#1 2016 3 5
pd.to_datetime(df)
#0 2015-02-04
#1 2016-03-05
#dtype: datetime64[ns]
pd.to_datetime(['11-12-2021'])
#DatetimeIndex(['2021-11-12'], dtype='datetime64[ns]', freq=None)
pd.to_datetime(["2005/11/23", "2010.12.31"])
#DatetimeIndex(['2005-11-23', '2010-12-31'], dtype='datetime64[ns]', freq=None)
pd.to_datetime([1349720105, 1349806505, 1349892905], unit="s")
#DatetimeIndex(['2012-10-08 18:15:05', '2012-10-09 18:15:05',
# '2012-10-10 18:15:05'],
# dtype='datetime64[ns]', freq=None)
pd.to_datetime([1349720105100, 1349720105200, 1349720105300], unit="ms")
#DatetimeIndex(['2012-10-08 18:15:05.100000', '2012-10-08 18:15:05.200000',
# '2012-10-08 18:15:05.300000'],
# dtype='datetime64[ns]', freq=None)
pd.to_datetime('210605')
#Timestamp('2005-06-21 00:00:00')
pd.to_datetime('210605',yearfirst=True)
#Timestamp('2021-06-05 00:00:00')
pd.to_datetime([1, 2, 3], unit='d', origin=pd.Timestamp('2020-01-11'))
#DatetimeIndex(['2020-01-12', '2020-01-13', '2020-01-14'], dtype='datetime64[ns]', freq=None)
pd.to_datetime([1, 2, 3], unit='h', origin=pd.Timestamp('2020-01'))
#DatetimeIndex(['2020-01-01 01:00:00', '2020-01-01 02:00:00',
# '2020-01-01 03:00:00'],
# dtype='datetime64[ns]', freq=None)
pd.to_datetime([1, 2, 3], unit='m', origin=pd.Timestamp('2020-01'))
#DatetimeIndex(['2020-01-01 00:01:00', '2020-01-01 00:02:00',
# '2020-01-01 00:03:00'],
# dtype='datetime64[ns]', freq=None)
示例 4:origin 参考起始时间 s(秒钟),生成后面的时间。
pd.to_datetime([1, 2, 3], unit='s', origin=pd.Timestamp('2020-01'))
#DatetimeIndex(['2020-01-01 00:00:01', '2020-01-01 00:00:02',
# '2020-01-01 00:00:03'],
# dtype='datetime64[ns]', freq=None)
ParserError: year 120211204 is out of range: 120211204
。pd.to_datetime(['120211204','20210101'])
pd.to_datetime(['202102.01','2021.02.01'], errors="ignore")
#Index(['202102.01', '2021.02.01'], dtype='object')
pd.to_datetime(['120211204','2021.02.01'], errors="coerce")
#DatetimeIndex(['NaT', '2021-02-01'], dtype='datetime64[ns]', freq=None)
pd.to_datetime(pd.Series(["Jul 31, 2018", "2018.05.10", None]))
#0 2018-07-31
#1 2018-05-10
#2 NaT
#dtype: datetime64[ns]
asfreq(freq, method=None, how=None, normalize=False, fill_value=None)
ts = pd.Series(np.random.rand(4),
index = pd.date_range('20170101','20170104'))
print(ts)
#2017-01-01 0.212259
#2017-01-02 0.475196
#2017-01-03 0.186119
#2017-01-04 0.355026
#Freq: D, dtype: float64
ts.asfreq('4H')
#2017-01-01 00:00:00 0.212259
#2017-01-01 04:00:00 NaN
#2017-01-01 08:00:00 NaN
#2017-01-01 12:00:00 NaN
#2017-01-01 16:00:00 NaN
#2017-01-01 20:00:00 NaN
#2017-01-02 00:00:00 0.475196
#2017-01-02 04:00:00 NaN
#2017-01-02 08:00:00 NaN
#2017-01-02 12:00:00 NaN
#2017-01-02 16:00:00 NaN
#2017-01-02 20:00:00 NaN
#2017-01-03 00:00:00 0.186119
#2017-01-03 04:00:00 NaN
#2017-01-03 08:00:00 NaN
#2017-01-03 12:00:00 NaN
#2017-01-03 16:00:00 NaN
#2017-01-03 20:00:00 NaN
#2017-01-04 00:00:00 0.355026
Freq: 4H, dtype: float64
ts.asfreq('4H', method = 'ffill')
#2017-01-01 00:00:00 0.212259
#2017-01-01 04:00:00 0.212259
#2017-01-01 08:00:00 0.212259
#2017-01-01 12:00:00 0.212259
#2017-01-01 16:00:00 0.212259
#2017-01-01 20:00:00 0.212259
#2017-01-02 00:00:00 0.475196
#2017-01-02 04:00:00 0.475196
#2017-01-02 08:00:00 0.475196
#2017-01-02 12:00:00 0.475196
#2017-01-02 16:00:00 0.475196
#2017-01-02 20:00:00 0.475196
#2017-01-03 00:00:00 0.186119
#32017-01-03 04:00:00 0.186119
#2017-01-03 08:00:00 0.186119
#2017-01-03 12:00:00 0.186119
#2017-01-03 16:00:00 0.186119
#2017-01-03 20:00:00 0.186119
#2017-01-04 00:00:00 0.355026
#Freq: 4H, dtype: float64
ts.asfreq('4H', method = 'bfill')
#2017-01-01 00:00:00 0.212259
#2017-01-01 04:00:00 0.475196
#2017-01-01 08:00:00 0.475196
#2017-01-01 12:00:00 0.475196
#2017-01-01 16:00:00 0.475196
#2017-01-01 20:00:00 0.475196
#2017-01-02 00:00:00 0.475196
#2017-01-02 04:00:00 0.186119
#2017-01-02 08:00:00 0.186119
#2017-01-02 12:00:00 0.186119
#2017-01-02 16:00:00 0.186119
#2017-01-02 20:00:00 0.186119
#2017-01-03 00:00:00 0.186119
#2017-01-03 04:00:00 0.355026
#2017-01-03 08:00:00 0.355026
#2017-01-03 12:00:00 0.355026
#2017-01-03 16:00:00 0.355026
#2017-01-03 20:00:00 0.355026
#2017-01-04 00:00:00 0.355026
#Freq: 4H, dtype: float64
shift(periods=1, freq=None, axis=0, fill_value=None)
df = pd.DataFrame(np.random.rand(16).reshape((4,4)),
index = pd.date_range('20210101','20210104'),
columns=list('ABCD'))
df
# A B C D
#2021-01-01 0.435600 0.899540 0.146171 0.543191
#2021-01-02 0.978700 0.824476 0.015656 0.862976
#2021-01-03 0.069611 0.503938 0.215017 0.677024
#2021-01-04 0.112235 0.352456 0.026572 0.103668
df.shift(periods=2)
# A B C D
#2021-01-01 NaN NaN NaN NaN
#2021-01-02 NaN NaN NaN NaN
#2021-01-03 0.4356 0.899540 0.146171 0.543191
#2021-01-04 0.9787 0.824476 0.015656 0.862976
df.shift(periods=1, axis="columns")
# A B C D
#2021-01-01 NaN 0.435600 0.899540 0.146171
#2021-01-02 NaN 0.978700 0.824476 0.015656
#2021-01-03 NaN 0.069611 0.503938 0.215017
#2021-01-04 NaN 0.112235 0.352456 0.026572
df.shift(periods=3, fill_value=0)
# A B C D
#2021-01-01 0.0000 0.00000 0.000000 0.000000
#2021-01-02 0.0000 0.00000 0.000000 0.000000
#2021-01-03 0.0000 0.00000 0.000000 0.000000
#2021-01-04 0.4356 0.89954 0.146171 0.543191
df.shift(periods=3, freq="D")
# A B C D
#2021-01-04 0.435600 0.899540 0.146171 0.543191
#2021-01-05 0.978700 0.824476 0.015656 0.862976
#2021-01-06 0.069611 0.503938 0.215017 0.677024
#2021-01-07 0.112235 0.352456 0.026572 0.103668
per = df/df.shift(1) - 1
print(per)
# A B C D
#2021-01-01 NaN NaN NaN NaN
#2021-01-02 1.246787 -0.083447 -0.892891 0.588714
#2021-01-03 -0.928874 -0.388779 12.733554 -0.215477
#2021-01-04 0.612317 -0.300595 -0.876419 -0.846877