import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
时间函数相关
to_datetime
to_datedelta
date_range
bdate_range
"""
to_datetime 常用格式
%d 日期 01, 02, …, 31
%m 月份 01,02,…,12
%Y 年份 2022
%H 24小时
%I 12小时
%M 分钟
%S 秒钟
"""
print(pd.to_datetime('20220102 12:21',format='%Y%m%d %H:%M'))
pd.to_datetime('20220102',format='%Y%m%d').strftime('%Y - %d - %m')
2022-01-02 12:21:00
'2022 - 02 - 01'
"""
to_timedelta 增加时间间隔
周:‘W’
天:‘D’ / ‘days’ / ‘day’
小时:‘hours’ / ‘hour’ / ‘hr’ / ‘h’
分钟:‘m’ / ‘minute’ / ‘min’ / ‘minutes’ / ‘T’
秒:‘S’ / ‘seconds’ / ‘sec’ / ‘second’
"""
pd.to_datetime('20220102',format='%Y%m%d') + pd.to_timedelta('1W'),
(Timestamp('2022-01-09 00:00:00'),)
pd.date_range(start='2022-08-01',end='2022-09-01',freq='D')
DatetimeIndex(['2022-08-01', '2022-08-02', '2022-08-03', '2022-08-04',
'2022-08-05', '2022-08-06', '2022-08-07', '2022-08-08',
'2022-08-09', '2022-08-10', '2022-08-11', '2022-08-12',
'2022-08-13', '2022-08-14', '2022-08-15', '2022-08-16',
'2022-08-17', '2022-08-18', '2022-08-19', '2022-08-20',
'2022-08-21', '2022-08-22', '2022-08-23', '2022-08-24',
'2022-08-25', '2022-08-26', '2022-08-27', '2022-08-28',
'2022-08-29', '2022-08-30', '2022-08-31', '2022-09-01'],
dtype='datetime64[ns]', freq='D')
pd.date_range(start='2022-08-01',end='2022-08-10',freq='B') == pd.bdate_range(start='2022-08-01',end='2022-08-10')
array([ True, True, True, True, True, True, True, True])
pd.Series(pd.bdate_range(start='2022-08-01',end='2022-08-10')).dt.quarter
0 3
1 3
2 3
3 3
4 3
5 3
6 3
7 3
dtype: int64
interpolate 缺失值线性填充函数
ts = pd.Series([1,np.nan,7,np.nan,np.nan,np.nan,2,np.nan,11,np.nan,9])
plt.figure(figsize=(9,5))
l1 = ts
x = np.arange(len(l1),)
for i in [ 'zero', 'slinear']:
plt.plot(x,ts.interpolate(method=i,order = 1),label = i,marker = 'o',markersize = 4)
plt.axvline(x=0,ls = '-.',lw = 0.5)
plt.axvline(x=2,ls = '-.',lw = 0.5)
plt.axvline(x=6,ls = '-.',lw = 0.5)
plt.axvline(x=8,ls = '-.',lw = 0.5)
plt.axvline(x=10,ls = '-.',lw = 0.5)
plt.legend(loc = 'upper left')
<matplotlib.legend.Legend at 0x23832342e50>
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-l2fTZr7b-1663211083506)(output_10_1.png)]](https://i-blog.csdnimg.cn/blog_migrate/f8784aaceeb69ca75d87481c65ca53d3.png)
plt.figure(figsize=(9,5))
l1 = ts
x = np.arange(len(l1),)
for i in [ 'quadratic', 'cubic','barycentric']:
plt.plot(x,ts.interpolate(method=i),label = i,marker = 'o',markersize = 4)
plt.axvline(x=0,ls = '-.',lw = 0.5)
plt.axvline(x=2,ls = '-.',lw = 0.5)
plt.axvline(x=6,ls = '-.',lw = 0.5)
plt.axvline(x=8,ls = '-.',lw = 0.5)
plt.axvline(x=10,ls = '-.',lw = 0.5)
plt.legend()
<matplotlib.legend.Legend at 0x2383bc6ff10>
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-pyK7CvoG-1663211083518)(output_11_1.png)]](https://i-blog.csdnimg.cn/blog_migrate/57c325ca639dba4835a51251f142bc2e.png)
plt.figure(figsize=(9,5))
l1 = ts
x = np.arange(len(l1),)
plt.axvline(x=0,ls = '-.',lw = 0.5)
plt.axvline(x=2,ls = '-.',lw = 0.5)
plt.axvline(x=6,ls = '-.',lw = 0.5)
plt.axvline(x=8,ls = '-.',lw = 0.5)
plt.axvline(x=10,ls = '-.',lw = 0.5)
for i in [1,2,3,4]:
plt.plot(x,ts.interpolate(method='spline',order = i),label = f'{i} level ',marker = 'o',markersize = 4)
plt.legend(loc = 'upper left')
<matplotlib.legend.Legend at 0x2383dd4a160>
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-lmxOGV9o-1663211083520)(output_12_1.png)]](https://i-blog.csdnimg.cn/blog_migrate/5c2be33750fe0d3bbc39099fd6382490.png)
plot
ts = pd.Series(np.random.randint(2,10,9),index=[f'col{i}' for i in range(9)])
ts.plot.pie()
<AxesSubplot:ylabel='None'>
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-tQU26JiN-1663211083522)(output_15_1.png)]](https://i-blog.csdnimg.cn/blog_migrate/06ea91b3f57289596eef006c527adc10.png)
ts.plot.bar()
<AxesSubplot:>
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-lV2mmY08-1663211083526)(output_16_1.png)]](https://i-blog.csdnimg.cn/blog_migrate/1437c5425619af23a66947ca3201fbff.png)
ts.plot.box()
<AxesSubplot:>
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-fLrP4k06-1663211083527)(output_17_1.png)]](https://i-blog.csdnimg.cn/blog_migrate/3d514df158f4dc8a12cbfcb4965c4e89.png)
ts.plot.line()
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-oqKNyuKp-1663211083529)(output_18_1.png)]](https://i-blog.csdnimg.cn/blog_migrate/0acf0751ddbc8c0b02b7847d6ded22c4.png)
other
df = pd.DataFrame({'col':np.arange(-10,10,5),'col1':np.random.randint(-10,10,4)})
df
df.col1.nunique()
4
df.col1.between(2,10)
0 False
1 True
2 True
3 False
Name: col1, dtype: bool
df.col1.clip(2,10)
0 2
1 6
2 7
3 2
Name: col1, dtype: int32
df.col.is_monotonic
True
df.col.idxmax(),df.col.argmax()
(3, 3)
df.col.sample(n=2)
2 0
3 5
Name: col, dtype: int32
df.col.duplicated()
0 False
1 False
2 False
3 False
Name: col, dtype: bool