Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从一列中查找缺少的小时数,并在Python中另存为txt文件_Python_Pandas_Dataframe_Numpy_Datetime - Fatal编程技术网

从一列中查找缺少的小时数,并在Python中另存为txt文件

从一列中查找缺少的小时数,并在Python中另存为txt文件,python,pandas,dataframe,numpy,datetime,Python,Pandas,Dataframe,Numpy,Datetime,给定如下数据集: 日期 二号 二氧化硫 臭氧 0 2018/11/14 10:00 9 25 80 1. 2018/11/14 12:00 9 26 88 2. 2018/11/14 13:00 8. 26 88 3. 2018/11/14 14:00 8. 34 88 4. 2018/11/14 15:00 8. 37 89 5. 2018/11/14 17:00 8. 72 40 6. 2018/11/14 18:00 8. 56 50 7. 2018/11/14 19:00 7. 81 2

给定如下数据集:

日期 二号 二氧化硫 臭氧 0 2018/11/14 10:00 9 25 80 1. 2018/11/14 12:00 9 26 88 2. 2018/11/14 13:00 8. 26 88 3. 2018/11/14 14:00 8. 34 88 4. 2018/11/14 15:00 8. 37 89 5. 2018/11/14 17:00 8. 72 40 6. 2018/11/14 18:00 8. 56 50 7. 2018/11/14 19:00 7. 81 22 使用“处理唯一日期时间”:

#create sorted DatetimeIndex
df['date'] = pd.to_datetime(df['date'])
df = df.set_index('date').sort_index()

#if possible duplicates
#df = df.resample('H').first()

#if not duplicates
df = df.asfreq('H')

missing = df[df['NO2'].isna()]
对于写入文件,可以先转换自定义格式的
DatetimeIndex
值,然后再按numpy或pandas写入:

s = missing.index.strftime('%Y/%m/%d %H:%M').to_series()

np.savetxt('./missing_date.txt', s, fmt='%s')


date
是否唯一?是的,它们是唯一的。
s.to_csv('./missing_date.txt', index=False)