Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/312.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 如何替换pandas中特定于时间序列数据帧的值?_Python_Pandas_Replace_Time Series - Fatal编程技术网

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

Python 如何替换pandas中特定于时间序列数据帧的值?,python,pandas,replace,time-series,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

我有下面的数据帧(日期/时间是多索引的),我想将(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]]

日期时间
2018-01-26  00:00:00    21.65
00:15:00南
00:30:00南
00:45:00南
01:00:00南
01:15:00南
01:30:00南
01:45:00南
02:00:00南
02:15:00南
02:30:00南
02:45:00南
03:00:00南
03:15:00南
03:30:00南
03:45:00南
04:00:00南
04:15:00南
04:30:00南
04:45:00南
05:00:00南
05:15:00南
05:30:00南
05:45:00南
06:00:00南
06:15:00南
06:30:00南
06:45:00南
07:00:00南
07:15:00南
07:30:00南
07:45:00南
08:00:00南
08:15:00南
08:30:00南
08:45:00南
09:00:00南
09:15:00南
09:30:00南
09:45:00南
10:00:00南
10:15:00南
10:30:00南
10:45:00南
11:00:00南
名称:temp,数据类型:float64
我如何才能做到这一点?

您可以使用:

或者,如果第二级是时间:

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

编辑:

对于指定数组,必须使用按长度重复的第一级唯一值:

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)

时间
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]
                                                          ---^^^

杰出的我还有一个问题。。如果我需要更改值[1,2,3,4,5,6,7,8,9]。。那么我如何应用ii呢?数据很好-在所有级别的
00:00:00
02:00:00
之间总是有相同长度的行?你能输入1~9个值作为numpy数组吗?这是
np.arange(1,10)
当多索引是datetine格式时你能检查一下如何更改cod吗?
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)
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
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]
                                                          ---^^^