python - 如何替换 pandas 中的时间序列数据帧特定值?

标签 python pandas replace time-series

我有下面的数据帧(日期/时间是多索引),我想将 (00:00:00~07:00:00) 中的列值替换为 numpy 数组:

[[ 21.63920663  21.62012822  20.9900515   21.23217008  21.19482458
   21.10839656  20.89631935  20.79977166  20.99176729  20.91567565
   20.87258765  20.76210464  20.50357827  20.55897631  20.38005033
   20.38227309  20.54460993  20.37707293  20.08279925  20.09955877
   20.02559575  20.12390737  20.2917257   20.20056711  20.1589065
   20.41302289  20.48000767  20.55604102  20.70255192]]

 

     date        time    
2018-01-26  00:00:00    21.65
            00:15:00      NaN
            00:30:00      NaN
            00:45:00      NaN
            01:00:00      NaN
            01:15:00      NaN
            01:30:00      NaN
            01:45:00      NaN
            02:00:00      NaN
            02:15:00      NaN
            02:30:00      NaN
            02:45:00      NaN
            03:00:00      NaN
            03:15:00      NaN
            03:30:00      NaN
            03:45:00      NaN
            04:00:00      NaN
            04:15:00      NaN
            04:30:00      NaN
            04:45:00      NaN
            05:00:00      NaN
            05:15:00      NaN
            05:30:00      NaN
            05:45:00      NaN
            06:00:00      NaN
            06:15:00      NaN
            06:30:00      NaN
            06:45:00      NaN
            07:00:00      NaN
            07:15:00      NaN
            07:30:00      NaN
            07:45:00      NaN
            08:00:00      NaN
            08:15:00      NaN
            08:30:00      NaN
            08:45:00      NaN
            09:00:00      NaN
            09:15:00      NaN
            09:30:00      NaN
            09:45:00      NaN
            10:00:00      NaN
            10:15:00      NaN
            10:30:00      NaN
            10:45:00      NaN
            11:00:00      NaN
Name: temp, dtype: float64
<class 'datetime.time'>

我该怎么做?

最佳答案

您可以使用slicers :

idx = pd.IndexSlice
df1.loc[idx[:, '00:00:00':'02:00:00'],:] = 1

或者如果第二级是时间:

import datetime

idx = pd.IndexSlice
df1.loc[idx[:, datetime.time(0, 0, 0):datetime.time(2, 0, 0)],:] = 1

示例:

print (df1)
                       aaa
date       time           
2018-01-26 00:00:00  21.65
           00:15:00    NaN
           00:30:00    NaN
           00:45:00    NaN
           01:00:00    NaN
           01:15:00    NaN
           01:30:00    NaN
           01:45:00    NaN
           02:00:00    NaN
           02:15:00    NaN
           02:30:00    NaN
           02:45:00    NaN
           03:00:00    NaN
2018-01-27 00:00:00   2.00
           00:15:00    NaN
           00:30:00    NaN
           00:45:00    NaN
           01:00:00    NaN
           01:15:00    NaN
           01:30:00    NaN
           01:45:00    NaN
           02:00:00    NaN
           02:15:00    NaN
           02:30:00    NaN
           02:45:00    NaN
           03:00:00    NaN
<小时/>
idx = pd.IndexSlice
df1.loc[idx[:, '00:00:00':'02:00:00'],:] = 1
print (df1)
                     aaa
date       time         
2018-01-26 00:00:00  1.0
           00:15:00  1.0
           00:30:00  1.0
           00:45:00  1.0
           01:00:00  1.0
           01:15:00  1.0
           01:30:00  1.0
           01:45:00  1.0
           02:00:00  1.0
           02:15:00  NaN
           02:30:00  NaN
           02:45:00  NaN
           03:00:00  NaN
2018-01-27 00:00:00  1.0
           00:15:00  1.0
           00:30:00  1.0
           00:45:00  1.0
           01:00:00  1.0
           01:15:00  1.0
           01:30:00  1.0
           01:45:00  1.0
           02:00:00  1.0
           02:15:00  NaN
           02:30:00  NaN
           02:45:00  NaN
           03:00:00  NaN

编辑:

对于分配数组,必须使用 numpy.tile 按第一级唯一值的长度重复:

df1.loc[idx[:, '00:00:00':'02:00:00'],:] = np.tile(np.arange(1, 10),len(df1.index.levels[0]))
print (df1)
                     aaa
date       time         
2018-01-26 00:00:00  1.0
           00:15:00  2.0
           00:30:00  3.0
           00:45:00  4.0
           01:00:00  5.0
           01:15:00  6.0
           01:30:00  7.0
           01:45:00  8.0
           02:00:00  9.0
           02:15:00  NaN
           02:30:00  NaN
           02:45:00  NaN
           03:00:00  NaN
2018-01-27 00:00:00  1.0
           00:15:00  2.0
           00:30:00  3.0
           00:45:00  4.0
           01:00:00  5.0
           01:15:00  6.0
           01:30:00  7.0
           01:45:00  8.0
           02:00:00  9.0
           02:15:00  NaN
           02:30:00  NaN
           02:45:00  NaN
           03:00:00  NaN

通过切片长度生成数组的更通用解决方案:

idx = pd.IndexSlice
len0 = df1.loc[idx[df1.index.levels[0][0], '00:00:00':'02:00:00'],:].shape[0]
len1 = len(df1.index.levels[0])
df1.loc[idx[:, '00:00:00':'02:00:00'],:] = np.tile(np.arange(1, len0 + 1), len1)
<小时/>

使用 time 进行测试s:

import datetime
idx = pd.IndexSlice
arr =np.tile(np.arange(1, 10),len(df1.index.levels[0]))
df1.loc[idx[:, datetime.time(0, 0, 0):datetime.time(2, 0, 0)],:] = arr
print (df1)
                     aaa
date       time         
2018-01-26 00:00:00  1.0
           00:15:00  2.0
           00:30:00  3.0
           00:45:00  4.0
           01:00:00  5.0
           01:15:00  6.0
           01:30:00  7.0
           01:45:00  8.0
           02:00:00  9.0
           02:15:00  NaN
           02:30:00  NaN
           02:45:00  NaN
           03:00:00  NaN
2018-01-27 00:00:00  1.0
           00:15:00  2.0
           00:30:00  3.0
           00:45:00  4.0
           01:00:00  5.0
           01:15:00  6.0
           01:30:00  7.0
           01:45:00  8.0
           02:00:00  9.0
           02:15:00  NaN
           02:30:00  NaN
           02:45:00  NaN
           03:00:00  NaN

编辑:

最后发现了问题 - 我的解决方案仅用一列 DataFrame ,但如果使用 Series需要删除一个: :

arr = np.array([[ 21.63920663, 21.62012822, 20.9900515, 21.23217008, 21.19482458, 21.10839656, 
                 20.89631935, 20.79977166, 20.99176729, 20.91567565, 20.87258765, 20.76210464,
                 20.50357827, 20.55897631, 20.38005033, 20.38227309, 20.54460993, 20.37707293, 
                 20.08279925, 20.09955877, 20.02559575, 20.12390737, 20.2917257, 20.20056711, 
                 20.1589065, 20.41302289, 20.48000767, 20.55604102, 20.70255192]])

import datetime
idx = pd.IndexSlice
df1.loc[idx[:, datetime.time(0, 0, 0): datetime.time(7, 0, 0)]] = arr[0]
                                                          ---^^^

关于python - 如何替换 pandas 中的时间序列数据帧特定值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48576286/

相关文章:

python - cpython-35m-x86_64-linux-gnu.so 是从哪个基本文件类型生成的

python - "Process finished with exit code 1"是什么意思?

python - 用于 e NetworkManager VPN 连接的 dbus 信号处理程序

python - 使用单词列表翻译成英语

javascript - 如何替换字符串中的所有字符?

python - Pandas 重采样 : forcing specific start time of time bars

python - Pandas read_table() 缺少行

python - 防止 Imputer 丢失值

python-3.x - 应用groupby后如何创建单独的df?

c# - 正则表达式 : replace a string