# datetime.timedelta 时间差 t1 = datetime.datetime(2017,10,1) print(t1) print("") tx = datetime.timedelta(100) # timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0) print(tx) print("") t2 = t1 + tx print(t2)
2017-10-01 00:00:00 100 days, 0:00:00 2018-01-09 00:00:00
2019-05-262019-05-26
# datetime.timedelta 时间差 t1 = datetime.datetime(2017,10,1) print(t1) print("") tx = datetime.timedelta(100) # timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0) print(tx) print("") t2 = t1 + tx print(t2)
2017-10-01 00:00:00 100 days, 0:00:00 2018-01-09 00:00:00
# datetime.timedelta 时间差 t1 = datetime.datetime(2017,10,1) print(t1) print("") tx = datetime.timedelta(100) # timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0) print(tx) print("") t2 = t1 + tx print(t2)
2017-10-01 00:00:00 100 days, 0:00:00 2018-01-09 00:00:00
time_list1 = ["20171019", "20181020", "bbbb", "20191021"] t1= pd.to_datetime(time_list1, errors="ignore") print(t1, type(t1)) print("") t2 = pd.to_datetime(time_list1, errors="coerce") print(t2)
Index(['20171019', '20181020', 'bbbb', '20191021'], dtype='object')pandas.core.indexes.base.index'=""> DatetimeIndex(['2017-10-19', '2018-10-20', 'NaT', '2019-10-21'], dtype='datetime64[ns]', freq=None)
2020-06-01 14:28:08.656056 2020-06-01 14:28:08.656056 2017-10-21 00:00:00
rng = pd.DatetimeIndex(["20160910", "11/06/2017", "20180821", "26/05/2019"]) print(rng) print(type(rng)) print("") print(rng[0], type(rng[0]))
DatetimeIndex(['2016-09-10', '2017-11-06', '2018-08-21', '2019-05-26'], dtype='datetime64[ns]', freq=None)pandas.core.indexes.datetimes.datetimeindex'=""> 2016-09-10 00:00:00 pandas._libs.tslibs.timestamps.timestamp'="">
st = pd.Series(np.random.rand(4), index=rng) # 把时间戳索引当成index print(st)
2016-09-10 0.835586 2017-11-06 0.223044 2018-08-21 0.950717 2019-05-26 0.013370 dtype: float64
st = pd.Series(np.random.rand(4), index=rng) # 把时间戳索引当成index print(st)
2016-09-10 0.835586 2017-11-06 0.223044 2018-08-21 0.950717 2019-05-26 0.013370 dtype: float64
st = pd.Series(np.random.rand(4), index=rng) # 把时间戳索引当成index print(st)
2016-09-10 0.835586 2017-11-06 0.223044 2018-08-21 0.950717 2019-05-26 0.013370 dtype: float64
t_index2 = pd.date_range(start="20181018", periods=10, name="t_index2") print(t_index2)
DatetimeIndex(['2018-10-18', '2018-10-19', '2018-10-20', '2018-10-21', '2018-10-22', '2018-10-23', '2018-10-24', '2018-10-25', '2018-10-26', '2018-10-27'], dtype='datetime64[ns]', name='t_index2', freq='D')
t_index3 = pd.date_range(end="20181018", periods=10, name="t_index3") print(t_index3)
DatetimeIndex(['2018-10-09', '2018-10-10', '2018-10-11', '2018-10-12', '2018-10-13', '2018-10-14', '2018-10-15', '2018-10-16', '2018-10-17', '2018-10-18'], dtype='datetime64[ns]', name='t_index3', freq='D')
t_index3 = pd.date_range(end="20181018", periods=10, name="t_index3") print(t_index3)
DatetimeIndex(['2018-10-09', '2018-10-10', '2018-10-11', '2018-10-12', '2018-10-13', '2018-10-14', '2018-10-15', '2018-10-16', '2018-10-17', '2018-10-18'], dtype='datetime64[ns]', name='t_index3', freq='D')
t_index3 = pd.date_range(end="20181018", periods=10, name="t_index3") print(t_index3)
DatetimeIndex(['2018-10-09', '2018-10-10', '2018-10-11', '2018-10-12', '2018-10-13', '2018-10-14', '2018-10-15', '2018-10-16', '2018-10-17', '2018-10-18'], dtype='datetime64[ns]', name='t_index3', freq='D')
t_index6 = pd.bdate_range(start="20191001", end="20191007", name="t_index6") print(t_index6)
DatetimeIndex(['2019-10-01', '2019-10-02', '2019-10-03', '2019-10-04', '2019-10-07'], dtype='datetime64[ns]', name='t_index6', freq='B')
DatetimeIndex(['2019-09-10', '2019-09-11', '2019-09-12', '2019-09-13', '2019-09-14', '2019-09-15', '2019-09-16', '2019-09-17'], dtype='datetime64[ns]', name='t_index5', freq='D')
t_index7_list= pd.date_range(start="20191001", end="20191007", name="t_index7_list") print(t_index7_list) print("\n") t_index7_list= list(pd.date_range(start="20191001", end="20191007", name="t_index7_list")) print(t_index7_list)
DatetimeIndex(['2019-10-01', '2019-10-02', '2019-10-03', '2019-10-04', '2019-10-05', '2019-10-06', '2019-10-07'], dtype='datetime64[ns]', name='t_index7_list', freq='D')
DatetimeIndex(['2019-09-10', '2019-09-11', '2019-09-12', '2019-09-13', '2019-09-14', '2019-09-15', '2019-09-16', '2019-09-17'], dtype='datetime64[ns]', name='t_index5', freq='D')
DatetimeIndex(['2019-09-11', '2019-09-12', '2019-09-13', '2019-09-14', '2019-09-15', '2019-09-16', '2019-09-17', '2019-09-18'], dtype='datetime64[ns]', name='t_index5', freq='D')
# 默认freq = 'D' 每日 pd.date_range("10/1/2019", "2019/10/7")
DatetimeIndex(['2019-10-01', '2019-10-02', '2019-10-03', '2019-10-04', '2019-10-05', '2019-10-06', '2019-10-07'], dtype='datetime64[ns]', freq='D')
# 'B' 每工作日 pd.date_range("10/01/2019", "10/07/2019", freq = "B")
DatetimeIndex(['2019-10-01', '2019-10-02', '2019-10-03', '2019-10-04', '2019-10-07'], dtype='datetime64[ns]', freq='B')
[Timestamp('2019-10-01 00:00:00', freq='D'), Timestamp('2019-10-02 00:00:00', freq='D'), Timestamp('2019-10-03 00:00:00', freq='D'), Timestamp('2019-10-04 00:00:00', freq='D'), Timestamp('2019-10-05 00:00:00', freq='D'), Timestamp('2019-10-06 00:00:00', freq='D'), Timestamp('2019-10-07 00:00:00', freq='D')]
# H 每小时 pd.date_range("10/01/2019 12:00:00", "10/02/2019 12:00:00", freq = "H")
DatetimeIndex(['2019-10-01 12:00:00', '2019-10-01 13:00:00', '2019-10-01 14:00:00', '2019-10-01 15:00:00', '2019-10-01 16:00:00', '2019-10-01 17:00:00', '2019-10-01 18:00:00', '2019-10-01 19:00:00', '2019-10-01 20:00:00', '2019-10-01 21:00:00', '2019-10-01 22:00:00', '2019-10-01 23:00:00', '2019-10-02 00:00:00', '2019-10-02 01:00:00', '2019-10-02 02:00:00', '2019-10-02 03:00:00', '2019-10-02 04:00:00', '2019-10-02 05:00:00', '2019-10-02 06:00:00', '2019-10-02 07:00:00', '2019-10-02 08:00:00', '2019-10-02 09:00:00', '2019-10-02 10:00:00', '2019-10-02 11:00:00', '2019-10-02 12:00:00'], dtype='datetime64[ns]', freq='H')
# T/MIN 每分 pd.date_range("10/01/2019 12:10:00" , "10/01/2019 12:30:00", freq = "T")
DatetimeIndex(['2019-10-01 12:10:00', '2019-10-01 12:11:00', '2019-10-01 12:12:00', '2019-10-01 12:13:00', '2019-10-01 12:14:00', '2019-10-01 12:15:00', '2019-10-01 12:16:00', '2019-10-01 12:17:00', '2019-10-01 12:18:00', '2019-10-01 12:19:00', '2019-10-01 12:20:00', '2019-10-01 12:21:00', '2019-10-01 12:22:00', '2019-10-01 12:23:00', '2019-10-01 12:24:00', '2019-10-01 12:25:00', '2019-10-01 12:26:00', '2019-10-01 12:27:00', '2019-10-01 12:28:00', '2019-10-01 12:29:00', '2019-10-01 12:30:00'], dtype='datetime64[ns]', freq='T')
# S 每秒 pd.date_range("10/01/2019", "10/01/2019 00:00:30", freq = "S")
DatetimeIndex(['2019-10-01 00:00:00', '2019-10-01 00:00:01', '2019-10-01 00:00:02', '2019-10-01 00:00:03', '2019-10-01 00:00:04', '2019-10-01 00:00:05', '2019-10-01 00:00:06', '2019-10-01 00:00:07', '2019-10-01 00:00:08', '2019-10-01 00:00:09', '2019-10-01 00:00:10', '2019-10-01 00:00:11', '2019-10-01 00:00:12', '2019-10-01 00:00:13', '2019-10-01 00:00:14', '2019-10-01 00:00:15', '2019-10-01 00:00:16', '2019-10-01 00:00:17', '2019-10-01 00:00:18', '2019-10-01 00:00:19', '2019-10-01 00:00:20', '2019-10-01 00:00:21', '2019-10-01 00:00:22', '2019-10-01 00:00:23', '2019-10-01 00:00:24', '2019-10-01 00:00:25', '2019-10-01 00:00:26', '2019-10-01 00:00:27', '2019-10-01 00:00:28', '2019-10-01 00:00:29', '2019-10-01 00:00:30'], dtype='datetime64[ns]', freq='S')
# L 每毫秒 (千分之一秒) pd.date_range("10/01/2019", "10/01/2019 00:00:30", freq = "L")
DatetimeIndex([ '2019-10-01 00:00:00', '2019-10-01 00:00:00.001000', '2019-10-01 00:00:00.002000', '2019-10-01 00:00:00.003000', '2019-10-01 00:00:00.004000', '2019-10-01 00:00:00.005000', '2019-10-01 00:00:00.006000', '2019-10-01 00:00:00.007000', '2019-10-01 00:00:00.008000', '2019-10-01 00:00:00.009000', ... '2019-10-01 00:00:29.991000', '2019-10-01 00:00:29.992000', '2019-10-01 00:00:29.993000', '2019-10-01 00:00:29.994000', '2019-10-01 00:00:29.995000', '2019-10-01 00:00:29.996000', '2019-10-01 00:00:29.997000', '2019-10-01 00:00:29.998000', '2019-10-01 00:00:29.999000', '2019-10-01 00:00:30'], dtype='datetime64[ns]', length=30001, freq='L')
# U 每微秒 (百万分之一秒) pd.date_range("10/01/2019", "10/01/2019 00:00:30", freq = "U") # U 每微秒 (百万分之一秒)
DatetimeIndex([ '2019-10-01 00:00:00', '2019-10-01 00:00:00.000001', '2019-10-01 00:00:00.000002', '2019-10-01 00:00:00.000003', '2019-10-01 00:00:00.000004', '2019-10-01 00:00:00.000005', '2019-10-01 00:00:00.000006', '2019-10-01 00:00:00.000007', '2019-10-01 00:00:00.000008', '2019-10-01 00:00:00.000009', ... '2019-10-01 00:00:29.999991', '2019-10-01 00:00:29.999992', '2019-10-01 00:00:29.999993', '2019-10-01 00:00:29.999994', '2019-10-01 00:00:29.999995', '2019-10-01 00:00:29.999996', '2019-10-01 00:00:29.999997', '2019-10-01 00:00:29.999998', '2019-10-01 00:00:29.999999', '2019-10-01 00:00:30'], dtype='datetime64[ns]', length=30000001, freq='U')
# U 每微秒 (百万分之一秒) pd.date_range("10/01/2019", "10/01/2019 00:00:30", freq = "U") # U 每微秒 (百万分之一秒)
DatetimeIndex([ '2019-10-01 00:00:00', '2019-10-01 00:00:00.000001', '2019-10-01 00:00:00.000002', '2019-10-01 00:00:00.000003', '2019-10-01 00:00:00.000004', '2019-10-01 00:00:00.000005', '2019-10-01 00:00:00.000006', '2019-10-01 00:00:00.000007', '2019-10-01 00:00:00.000008', '2019-10-01 00:00:00.000009', ... '2019-10-01 00:00:29.999991', '2019-10-01 00:00:29.999992', '2019-10-01 00:00:29.999993', '2019-10-01 00:00:29.999994', '2019-10-01 00:00:29.999995', '2019-10-01 00:00:29.999996', '2019-10-01 00:00:29.999997', '2019-10-01 00:00:29.999998', '2019-10-01 00:00:29.999999', '2019-10-01 00:00:30'], dtype='datetime64[ns]', length=30000001, freq='U')
# U 每微秒 (百万分之一秒) pd.date_range("10/01/2019", "10/01/2019 00:00:30", freq = "U") # U 每微秒 (百万分之一秒)
DatetimeIndex([ '2019-10-01 00:00:00', '2019-10-01 00:00:00.000001', '2019-10-01 00:00:00.000002', '2019-10-01 00:00:00.000003', '2019-10-01 00:00:00.000004', '2019-10-01 00:00:00.000005', '2019-10-01 00:00:00.000006', '2019-10-01 00:00:00.000007', '2019-10-01 00:00:00.000008', '2019-10-01 00:00:00.000009', ... '2019-10-01 00:00:29.999991', '2019-10-01 00:00:29.999992', '2019-10-01 00:00:29.999993', '2019-10-01 00:00:29.999994', '2019-10-01 00:00:29.999995', '2019-10-01 00:00:29.999996', '2019-10-01 00:00:29.999997', '2019-10-01 00:00:29.999998', '2019-10-01 00:00:29.999999', '2019-10-01 00:00:30'], dtype='datetime64[ns]', length=30000001, freq='U')
# M -- 每月最后一个日历日 pd.date_range("2019", "2020", freq = "M")
DatetimeIndex(['2019-01-31', '2019-02-28', '2019-03-31', '2019-04-30', '2019-05-31', '2019-06-30', '2019-07-31', '2019-08-31', '2019-09-30', '2019-10-31', '2019-11-30', '2019-12-31'], dtype='datetime64[ns]', freq='M')
# M -- 每月最后一个日历日 pd.date_range("2019", "2020", freq = "M")
DatetimeIndex(['2019-01-31', '2019-02-28', '2019-03-31', '2019-04-30', '2019-05-31', '2019-06-30', '2019-07-31', '2019-08-31', '2019-09-30', '2019-10-31', '2019-11-30', '2019-12-31'], dtype='datetime64[ns]', freq='M')
# M -- 每月最后一个日历日 pd.date_range("2019", "2020", freq = "M")
DatetimeIndex(['2019-01-31', '2019-02-28', '2019-03-31', '2019-04-30', '2019-05-31', '2019-06-30', '2019-07-31', '2019-08-31', '2019-09-30', '2019-10-31', '2019-11-30', '2019-12-31'], dtype='datetime64[ns]', freq='M')
# BM - 每月最后一个工作日 print(pd.date_range("2019", "2020", freq="BM"))
DatetimeIndex(['2019-01-31', '2019-02-28', '2019-03-29', '2019-04-30', '2019-05-31', '2019-06-28', '2019-07-31', '2019-08-30', '2019-09-30', '2019-10-31', '2019-11-29', '2019-12-31'], dtype='datetime64[ns]', freq='BM')
DatetimeIndex(['2019-01-31', '2019-04-30', '2019-07-31', '2019-10-31'], dtype='datetime64[ns]', freq='Q-JAN') DatetimeIndex(['2019-02-28', '2019-05-31', '2019-08-31', '2019-11-30'], dtype='datetime64[ns]', freq='Q-FEB') DatetimeIndex(['2019-03-31', '2019-06-30', '2019-09-30', '2019-12-31'], dtype='datetime64[ns]', freq='Q-MAR') DatetimeIndex(['2019-01-31', '2019-04-30', '2019-07-31', '2019-10-31'], dtype='datetime64[ns]', freq='Q-APR')
# BQ - 每个季度末最后一月的最后一个工作日 print(pd.date_range("2019", "2021", freq="BQ-JAN")) print("") print(pd.date_range("2019", "2021", freq="BQ-FEB")) print("") print(pd.date_range("2019", "2021", freq="BQ-MAR")) print("") print(pd.date_range("2019", "2021", freq="BQ-APR"))
DatetimeIndex(['2019-01-31', '2019-04-30', '2019-07-31', '2019-10-31', '2020-01-31', '2020-04-30', '2020-07-31', '2020-10-30'], dtype='datetime64[ns]', freq='BQ-JAN') DatetimeIndex(['2019-02-28', '2019-05-31', '2019-08-30', '2019-11-29', '2020-02-28', '2020-05-29', '2020-08-31', '2020-11-30'], dtype='datetime64[ns]', freq='BQ-FEB') DatetimeIndex(['2019-03-29', '2019-06-28', '2019-09-30', '2019-12-31', '2020-03-31', '2020-06-30', '2020-09-30', '2020-12-31'], dtype='datetime64[ns]', freq='BQ-MAR') DatetimeIndex(['2019-01-31', '2019-04-30', '2019-07-31', '2019-10-31', '2020-01-31', '2020-04-30', '2020-07-31', '2020-10-30'], dtype='datetime64[ns]', freq='BQ-APR')
# BA -- 每年指定月份的最后一个工作日 print(pd.date_range("2019", "2021", freq="BA-JAN")) print(pd.date_range("2019", "2023", freq="BA-FEB")) print(pd.date_range("2019", "2021", freq="BA-MAR"))
DatetimeIndex(['2019-01-31', '2020-01-31'], dtype='datetime64[ns]', freq='BA-JAN') DatetimeIndex(['2019-02-28', '2020-02-28', '2021-02-26', '2022-02-28'], dtype='datetime64[ns]', freq='BA-FEB') DatetimeIndex(['2019-03-29', '2020-03-31'], dtype='datetime64[ns]', freq='BA-MAR')
# MS -- 每月第一个日历日 pd.date_range("2019", "2020", freq="MS")
DatetimeIndex(['2019-01-01', '2019-02-01', '2019-03-01', '2019-04-01', '2019-05-01', '2019-06-01', '2019-07-01', '2019-08-01', '2019-09-01', '2019-10-01', '2019-11-01', '2019-12-01', '2020-01-01'], dtype='datetime64[ns]', freq='MS')
# QS - 每个季度末最后一月的第一个日历日 print(pd.date_range("2019", "2020", freq="QS-JAN")) print("") print(pd.date_range("2019", "2020", freq="QS-FEB")) print("") print(pd.date_range("2019", "2020", freq="QS-MAR")) print("") print(pd.date_range("2019", "2020", freq="QS-APR"))
DatetimeIndex(['2019-01-01', '2019-04-01', '2019-07-01', '2019-10-01', '2020-01-01'], dtype='datetime64[ns]', freq='QS-JAN') DatetimeIndex(['2019-02-01', '2019-05-01', '2019-08-01', '2019-11-01'], dtype='datetime64[ns]', freq='QS-FEB') DatetimeIndex(['2019-03-01', '2019-06-01', '2019-09-01', '2019-12-01'], dtype='datetime64[ns]', freq='QS-MAR') DatetimeIndex(['2019-01-01', '2019-04-01', '2019-07-01', '2019-10-01', '2020-01-01'], dtype='datetime64[ns]', freq='QS-APR')
# AS -- 每年指定月份的第一个日历日 print(pd.date_range("2019", "2021", freq="AS-JAN")) print(pd.date_range("2019", "2021", freq="AS-FEB")) print(pd.date_range("2019", "2021", freq="AS-DEC"))
DatetimeIndex(['2019-01-01', '2020-01-01', '2021-01-01'], dtype='datetime64[ns]', freq='AS-JAN') DatetimeIndex(['2019-02-01', '2020-02-01'], dtype='datetime64[ns]', freq='AS-FEB') DatetimeIndex(['2019-12-01', '2020-12-01'], dtype='datetime64[ns]', freq='AS-DEC')
# BMS -- 每月第一个工作日 print(pd.date_range("2019", "2021", freq="BMS"))
DatetimeIndex(['2019-01-01', '2019-02-01', '2019-03-01', '2019-04-01', '2019-05-01', '2019-06-03', '2019-07-01', '2019-08-01', '2019-09-02', '2019-10-01', '2019-11-01', '2019-12-02', '2020-01-01', '2020-02-03', '2020-03-02', '2020-04-01', '2020-05-01', '2020-06-01', '2020-07-01', '2020-08-03', '2020-09-01', '2020-10-01', '2020-11-02', '2020-12-01', '2021-01-01'], dtype='datetime64[ns]', freq='BMS')
# BQS - 每个季度末最后一月的第一个工作日 print(pd.date_range("2019", "2020", freq="BQS-JAN")) print("") print(pd.date_range("2019", "2020", freq="BQS-FEB")) print("") print(pd.date_range("2019", "2020", freq="BQS-MAR")) print("") print(pd.date_range("2019", "2020", freq="BQS-APR"))
DatetimeIndex(['2019-01-01', '2019-04-01', '2019-07-01', '2019-10-01', '2020-01-01'], dtype='datetime64[ns]', freq='BQS-JAN') DatetimeIndex(['2019-02-01', '2019-05-01', '2019-08-01', '2019-11-01'], dtype='datetime64[ns]', freq='BQS-FEB') DatetimeIndex(['2019-03-01', '2019-06-03', '2019-09-02', '2019-12-02'], dtype='datetime64[ns]', freq='BQS-MAR') DatetimeIndex(['2019-01-01', '2019-04-01', '2019-07-01', '2019-10-01', '2020-01-01'], dtype='datetime64[ns]', freq='BQS-APR')
# BAS -- 每年指定月份的第一个工作日 print(pd.date_range("2019", "2021", freq="BAS-JAN")) print(pd.date_range("2019", "2021", freq="BAS-FEB")) print(pd.date_range("2019", "2021", freq="BAS-DEC"))
DatetimeIndex(['2019-01-01', '2020-01-01', '2021-01-01'], dtype='datetime64[ns]', freq='BAS-JAN') DatetimeIndex(['2019-02-01', '2020-02-03'], dtype='datetime64[ns]', freq='BAS-FEB') DatetimeIndex(['2019-12-02', '2020-12-01'], dtype='datetime64[ns]', freq='BAS-DEC')
# BAS -- 每年指定月份的第一个工作日 print(pd.date_range("2019", "2021", freq="BAS-JAN")) print(pd.date_range("2019", "2021", freq="BAS-FEB")) print(pd.date_range("2019", "2021", freq="BAS-DEC"))
DatetimeIndex(['2019-01-01', '2020-01-01', '2021-01-01'], dtype='datetime64[ns]', freq='BAS-JAN') DatetimeIndex(['2019-02-01', '2020-02-03'], dtype='datetime64[ns]', freq='BAS-FEB') DatetimeIndex(['2019-12-02', '2020-12-01'], dtype='datetime64[ns]', freq='BAS-DEC')
# BAS -- 每年指定月份的第一个工作日 print(pd.date_range("2019", "2021", freq="BAS-JAN")) print(pd.date_range("2019", "2021", freq="BAS-FEB")) print(pd.date_range("2019", "2021", freq="BAS-DEC"))
DatetimeIndex(['2019-01-01', '2020-01-01', '2021-01-01'], dtype='datetime64[ns]', freq='BAS-JAN') DatetimeIndex(['2019-02-01', '2020-02-03'], dtype='datetime64[ns]', freq='BAS-FEB') DatetimeIndex(['2019-12-02', '2020-12-01'], dtype='datetime64[ns]', freq='BAS-DEC')
# 2M 每间隔2个月最后一个日历 pd.date_range("2019", "2021", freq="2M")
DatetimeIndex(['2019-01-31', '2019-03-31', '2019-05-31', '2019-07-31', '2019-09-30', '2019-11-30', '2020-01-31', '2020-03-31', '2020-05-31', '2020-07-31', '2020-09-30', '2020-11-30'], dtype='datetime64[ns]', freq='2M')
# 2h30min 间隔是2小时30分钟 pd.date_range("2019/10/1 00:00:00", "2019/10/1 12:00:00", freq="2h30min")
DatetimeIndex(['2019-10-01 00:00:00', '2019-10-01 02:30:00', '2019-10-01 05:00:00', '2019-10-01 07:30:00', '2019-10-01 10:00:00'], dtype='datetime64[ns]', freq='150T')
# 2M 每间隔2个月最后一个日历 pd.date_range("2019", "2021", freq="2M")
DatetimeIndex(['2019-01-31', '2019-03-31', '2019-05-31', '2019-07-31', '2019-09-30', '2019-11-30', '2020-01-31', '2020-03-31', '2020-05-31', '2020-07-31', '2020-09-30', '2020-11-30'], dtype='datetime64[ns]', freq='2M')
ts = pd.Series(np.random.rand(4), index=pd.date_range("2019/1/1", "2019/1/4")) print(ts) print("\n") # 这里是把D改为4H print(ts.asfreq("4H")) print("\n") # method 插值模式 ffill 用之前值填充 bfill 用之后值填充 print(ts.asfreq("4H", method="ffill")) print("\n") print(ts.asfreq("4H", method="bfill"))
2019-01-01 0.610403 2019-01-02 0.416557 2019-01-03 0.821631 2019-01-04 0.699457 Freq: D, dtype: float64
2019-01-01 00:00:00 0.610403 2019-01-01 04:00:00 NaN 2019-01-01 08:00:00 NaN 2019-01-01 12:00:00 NaN 2019-01-01 16:00:00 NaN 2019-01-01 20:00:00 NaN 2019-01-02 00:00:00 0.416557 2019-01-02 04:00:00 NaN 2019-01-02 08:00:00 NaN 2019-01-02 12:00:00 NaN 2019-01-02 16:00:00 NaN 2019-01-02 20:00:00 NaN 2019-01-03 00:00:00 0.821631 2019-01-03 04:00:00 NaN 2019-01-03 08:00:00 NaN 2019-01-03 12:00:00 NaN 2019-01-03 16:00:00 NaN 2019-01-03 20:00:00 NaN 2019-01-04 00:00:00 0.699457 Freq: 4H, dtype: float64
2019-01-01 00:00:00 0.610403 2019-01-01 04:00:00 0.610403 2019-01-01 08:00:00 0.610403 2019-01-01 12:00:00 0.610403 2019-01-01 16:00:00 0.610403 2019-01-01 20:00:00 0.610403 2019-01-02 00:00:00 0.416557 2019-01-02 04:00:00 0.416557 2019-01-02 08:00:00 0.416557 2019-01-02 12:00:00 0.416557 2019-01-02 16:00:00 0.416557 2019-01-02 20:00:00 0.416557 2019-01-03 00:00:00 0.821631 2019-01-03 04:00:00 0.821631 2019-01-03 08:00:00 0.821631 2019-01-03 12:00:00 0.821631 2019-01-03 16:00:00 0.821631 2019-01-03 20:00:00 0.821631 2019-01-04 00:00:00 0.699457 Freq: 4H, dtype: float64
2019-01-01 00:00:00 0.610403 2019-01-01 04:00:00 0.416557 2019-01-01 08:00:00 0.416557 2019-01-01 12:00:00 0.416557 2019-01-01 16:00:00 0.416557 2019-01-01 20:00:00 0.416557 2019-01-02 00:00:00 0.416557 2019-01-02 04:00:00 0.821631 2019-01-02 08:00:00 0.821631 2019-01-02 12:00:00 0.821631 2019-01-02 16:00:00 0.821631 2019-01-02 20:00:00 0.821631 2019-01-03 00:00:00 0.821631 2019-01-03 04:00:00 0.699457 2019-01-03 08:00:00 0.699457 2019-01-03 12:00:00 0.699457 2019-01-03 16:00:00 0.699457 2019-01-03 20:00:00 0.699457 2019-01-04 00:00:00 0.699457 Freq: 4H, dtype: float64
ts = pd.Series(np.random.rand(4), index=pd.date_range("2019/1/1", "2019/1/4")) print(ts) print("\n") print(ts.shift(1)) print("\n") print(ts.shift(-2)) print("\n") # 计算变化百分比 该时间戳的值与上一个时间戳的值相比 per = ts/ts.shift(1) print(per)
2019-01-01 0.197884 2019-01-02 0.403093 2019-01-03 0.208341 2019-01-04 0.330873 Freq: D, dtype: float64
2019-01-01 NaN 2019-01-02 0.197884 2019-01-03 0.403093 2019-01-04 0.208341 Freq: D, dtype: float64
2019-01-01 0.208341 2019-01-02 0.330873 2019-01-03 NaN 2019-01-04 NaN Freq: D, dtype: float64
2019-01-01 NaN 2019-01-02 2.037017 2019-01-03 0.516855 2019-01-04 1.588134 Freq: D, dtype: float64
print(ts) print("\n") print(ts.shift(2, freq="D")) # 按天 print("\n") print(ts.shift(2, freq="T")) # 按分钟
2019-01-01 0.197884 2019-01-02 0.403093 2019-01-03 0.208341 2019-01-04 0.330873 Freq: D, dtype: float64
2019-01-03 0.197884 2019-01-04 0.403093 2019-01-05 0.208341 2019-01-06 0.330873 Freq: D, dtype: float64
2019-01-01 00:02:00 0.197884 2019-01-02 00:02:00 0.403093 2019-01-03 00:02:00 0.208341 2019-01-04 00:02:00 0.330873 Freq: D, dtype: float64
(1)获取更多优质内容及精彩资讯,可前往:https://www.cda.cn/?seo
(2)了解更多数据领域的优质课程:
数据分析咨询请扫描二维码
在准备数据分析师面试时,掌握高频考题及其解答是应对面试的关键。为了帮助大家轻松上岸,以下是10个高频考题及其详细解析,外加 ...
2024-12-20互联网数据分析师是一个热门且综合性的职业,他们通过数据挖掘和分析,为企业的业务决策和运营优化提供强有力的支持。尤其在如今 ...
2024-12-20在现代商业环境中,数据分析师是不可或缺的角色。他们的工作不仅仅是对数据进行深入分析,更是协助企业从复杂的数据信息中提炼出 ...
2024-12-20随着大数据时代的到来,数据驱动的决策方式开始受到越来越多企业的青睐。近年来,数据分析在人力资源管理中正在扮演着至关重要的 ...
2024-12-20在数据分析的世界里,表面上的技术操作只是“入门票”,而真正的高手则需要打破一些“看不见的墙”。这些“隐形天花板”限制了数 ...
2024-12-19在数据分析领域,尽管行业前景广阔、岗位需求旺盛,但实际的工作难度却远超很多人的想象。很多新手初入数据分析岗位时,常常被各 ...
2024-12-19入门数据分析,许多人都会感到“难”,但这“难”究竟难在哪儿?对于新手而言,往往不是技术不行,而是思维方式、业务理解和实践 ...
2024-12-19在如今的行业动荡背景下,数据分析师的职业前景虽然面临一些挑战,但也充满了许多新的机会。随着技术的不断发展和多领域需求的提 ...
2024-12-19在信息爆炸的时代,数据分析师如同探险家,在浩瀚的数据海洋中寻觅有价值的宝藏。这不仅需要技术上的过硬实力,还需要一种艺术家 ...
2024-12-19在当今信息化社会,大数据已成为各行各业不可或缺的宝贵资源。大数据专业应运而生,旨在培养具备扎实理论基础和实践能力,能够应 ...
2024-12-19阿里P8、P9失业都找不到工作?是我们孤陋寡闻还是世界真的已经“癫”成这样了? 案例一:本硕都是 985,所学的专业也是当红专业 ...
2024-12-19CDA持证人Louis CDA持证人基本情况 我大学是在一个二线城市的一所普通二本院校读的,专业是旅游管理,非计算机非统计学。毕业之 ...
2024-12-18最近,知乎上有个很火的话题:“一个人为何会陷入社会底层”? 有人说,这个世界上只有一个分水岭,就是“羊水”;还有人说,一 ...
2024-12-18在这个数据驱动的时代,数据分析师的技能需求快速增长。掌握适当的编程语言不仅能增强分析能力,还能帮助分析师从海量数据中提取 ...
2024-12-17在当今信息爆炸的时代,数据分析已经成为许多行业中不可或缺的一部分。想要在这个领域脱颖而出,除了热情和毅力外,你还需要掌握 ...
2024-12-17数据分析,是一项通过科学方法处理数据以获取洞察并支持决策的艺术。无论是在商业环境中提升业绩,还是在科研领域推动创新,数据 ...
2024-12-17在数据分析领域,图表是我们表达数据故事的重要工具。它们不仅让数据变得更加直观,也帮助我们更好地理解数据中的趋势和模式。相 ...
2024-12-16在当今社会,我们身处着一个飞速发展、变化迅猛的时代。不同行业在科技进步、市场需求和政策支持的推动下蓬勃发展,呈现出令人瞩 ...
2024-12-16在现代商业世界中,数据分析师扮演着至关重要的角色。他们通过解析海量数据,为企业战略决策提供有力支持。要有效完成这项任务, ...
2024-12-16在当今数据爆炸的时代,数据分析师是组织中不可或缺的导航者。他们通过从大量数据中提取可操作的洞察力,帮助企业在竞争激烈的市 ...
2024-12-16