import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
dtime = pd.date_range(start="20171201",end="20180131",freq="D")
print(type(dtime))
print(dtime)
dtime = pd.date_range(start="20171201",end="20180131",freq="10D")
print(dtime)
dtime = pd.date_range(start="20171201",periods=10,freq="M")
print(dtime)
dtime = pd.date_range(start="2017-12-01 10:10:10",periods=50,freq="H")
print(dtime)
data = pd.DataFrame(np.arange(100).reshape(50,2),index=dtime,columns=list("AB"))
print(data)
#降採樣
resample_data = data.resample("D").count()
print(type(resample_data),"\n",resample_data)
#將時間戳轉爲字符串
time_index = resample_data["A"].index
time_str = [i.strftime("%Y%m%d") for i in time_index]
print(time_str)
#將字符串轉爲時間戳
print(pd.to_datetime(time_str))
read_data = pd.read_csv("./BeijingPM20100101_20151231.csv")
#整合時間字段
datetime = pd.PeriodIndex(year=read_data["year"],month=read_data["month"],day=read_data["day"],hour=read_data["hour"],freq="H")
read_data["datetime"] = datetime
read_data.set_index("datetime",inplace=True)
#數據中含有nan,但不影響求平均數
resample_data = read_data.resample("7D").mean()
pm_CN = resample_data["PM_Dongsi"]
pm_US = resample_data["PM_US Post"]
x_ticks = [i.strftime("%Y%m%d") for i in pm_CN.index]
plt.figure(figsize=(20,8),dpi=80)
plt.plot(x_ticks,pm_CN,label="PM_CN")
plt.plot(x_ticks,pm_US,label="PM_US")
plt.xticks(x_ticks[::10],rotation=45)
plt.legend(loc="best")
plt.show()