首页 > 解决方案 > 如何从数据框中选择具有相同日期时间的数据?

问题描述

我正在使用熊猫分析遥测数据。数据保存在 HDF5 文件中。当然,数据也有保存的时候。所以我的问题是如何从日期帧(甚至秒)中提取具有相同日期时间的数据?例如数据如下所示:

943             Power.PDM_1__PDM_Temperature_degC 2019-06-13 00:17:50    8.965
944                    Power.PDM_2__PDM_Current_A 2019-06-13 00:17:50    0.174
945  Power.BCM0_Battery_Interface_Plate_Temp_degC 2019-06-13 00:18:20   19.829
946      Power.BCM1_Battery_Cell_Temperature_degC 2019-06-13 00:18:20   79.146
947      Power.BCM2_Battery_Cell_Temperature_degC 2019-06-13 00:18:20   81.280
948                  Power.BCR0__Array_Current_mA 2019-06-13 00:18:20  561.000
949            Power.BCR0__Array_Temperature_degC 2019-06-13 00:18:20    5.920

所以如果数据是 2019-06-13 00:18:20,我希望只有这个数据:

945  Power.BCM0_Battery_Interface_Plate_Temp_degC 2019-06-13 00:18:20   19.829
    946      Power.BCM1_Battery_Cell_Temperature_degC 2019-06-13 00:18:20   79.146
    947      Power.BCM2_Battery_Cell_Temperature_degC 2019-06-13 00:18:20   81.280
    948                  Power.BCR0__Array_Current_mA 2019-06-13 00:18:20  561.000
    949            Power.BCR0__Array_Temperature_degC 2019-06-13 00:18:20    5.920

到目前为止我有这个代码:

file = pd.read_hdf('KazSTSAT5.h5', mode = 'r')
#reads HDF5 file

df = pd.DataFrame (file)

#converts UNIX type data to Usual datetime
df['time'] = pd.to_datetime(df['time'],unit='s')
pd.set_option('display.max_rows', None)

timestamp = df.iloc[0, 1]

time = pd.DataFrame()

标签: pythonpandas

解决方案


假设您已索引日期列。

ts = pd.Timestamp
print(df.query('time == @ts("20190613T001820")')

推荐阅读