如何使用最接近DateTime系列的DateTimes提取DataFrame行

时间:2018-09-23 02:05:01

标签: python-3.x pandas

以下示例代码提取日期时间序列最接近日期时间系列(merge_asof的参数1)的数据帧行。

import numpy as np
import pandas as pd

times = [1537625150.819065, 1537625150.919615, 1537625151.019658, 1537625151.120535,
         1537625151.220896, 1537625151.320936, 1537625151.420966, 1537625151.520997,
         1537625151.621964, 1537625151.721997, 1537625151.822350, 1537625151.923238,
         1537625152.023616, 1537625152.123638, 1537625152.224127, 1537625152.324161,
         1537625152.424821, 1537625152.525090, 1537625152.625157, 1537625152.725612]

df = pd.DataFrame({'time': times})
df['dt'] = pd.to_datetime((df.time.values * 1e6).astype(np.int64) * 1000)
df.set_index(df.dt, inplace=True)

start = pd.to_datetime(1537625151, unit='s')
end = pd.to_datetime(1537625152, unit='s')

new_df = pd.merge_asof(pd.DataFrame({'desired_dt': pd.date_range(start, end, freq='.25S')}),
                       df[start:end], left_on='desired_dt', right_on='dt', direction='nearest')

print(new_df)

输出如下:

               desired_dt                         dt
0 2018-09-22 14:05:51.000 2018-09-22 14:05:51.019658
1 2018-09-22 14:05:51.250 2018-09-22 14:05:51.220896
2 2018-09-22 14:05:51.500 2018-09-22 14:05:51.520997
3 2018-09-22 14:05:51.750 2018-09-22 14:05:51.721997
4 2018-09-22 14:05:52.000 2018-09-22 14:05:51.923238                               

代码完全按照我想要的去做,但是我收到以下警告:

FutureWarning:'dt'既是索引级别又是列标签。 默认为列,但是在将来的版本中这会引起歧义错误fill_method = fill_method)

我该如何解决这个问题,使其在将来的熊猫版本中起作用,或者有更好的方法吗?

0 个答案:

没有答案