替换NaN返回ValueError:条件数组必须与self的形状相同 - python

我的目的是使用“填充”(如果它们在上午7点之前发生)和“内插”(对于错误> = 7 am)来估算误差值(零和负)。我的“文本”文件包含数千天和数百列。下面的一小部分显示了三天,早上7点前后都有错误。

date                 a    b    c        
2016-03-02 06:55:00  0.0  1.0  0.0
2016-03-02 07:00:00  2.0  2.0  0.0
2016-03-02 07:55:00  3.0  0.0  3.0
2016-03-03 06:10:00 -4.0  4.0  0.0
2016-03-03 07:00:00  5.0  5.0  5.0
2016-03-03 07:05:00  6.0  0.0  6.0
2016-03-03 08:05:00  7.0  0.0  7.0
2016-03-03 17:40:00  8.0  8.0 -8.0
2016-03-04 05:55:00  0.0  9.0  0.0
2016-03-04 06:00:00  0.0  0.0  10.0

当“日期”为一列时,以下another post中的一小部分代码可以与其他df完美配合。

df['date'] = pd.to_datetime(df['date'])
df.set_index('date', inplace=True)

# Change zeros and negatives to NaN
df.replace(0, np.nan, inplace=True)  
df[df < 0] = np.nan                  

# construct Boolean switch series
switch = (df.index - df.index.normalize()) > pd.to_timedelta('07:00:00')

# use numpy.where to differentiate between two scenarios
df.iloc[:, 0:] = df.iloc[:, 0:].interpolate().where(switch, df.iloc[:, 0:].ffill())

但是,当“日期”成为索引时,代码将返回ValueError: Array conditional must be same shape as self。任何帮助表示赞赏。

参考方案

以下终于解决了我的问题。

df['date'] = pd.to_datetime(df['date'])
# don't set column 'date' to index

# Change zeros and negatives to NaN
df.replace(0, np.nan, inplace=True)  
df[df.loc[:, df.columns != 'date'] < 0] = np.nan # change negatives to NaN,   
                                                 # but exclude column 'date',   
                                                 # otherwise, column 'date' will be   
                                                 # converted to NaT  

# construct Boolean switch series
switch = (df['date'] - df['date'].dt.normalize()) > pd.to_timedelta('07:00:00')

# use numpy.where to differentiate between two scenarios
df.iloc[:, 0:] = df.iloc[:, 0:].interpolate().where(switch, df.iloc[:, 0:].ffill())

感谢@jpp建议最重要的后两行here。

R'relaimpo'软件包的Python端口 - python

我需要计算Lindeman-Merenda-Gold(LMG)分数,以进行回归分析。我发现R语言的relaimpo包下有该文件。不幸的是,我对R没有任何经验。我检查了互联网,但找不到。这个程序包有python端口吗?如果不存在,是否可以通过python使用该包? python参考方案 最近,我遇到了pingouin库。

我不明白为什么sum(df ['series'])!= df ['series']。sum() - python

我正在汇总一系列值,但是根据我的操作方式,我会得到不同的结果。我尝试过的两种方法是:sum(df['series']) df['series'].sum() 他们为什么会返回不同的值?示例代码。s = pd.Series([ 0.428229 , -0.948957 , -0.110125 , 0.791305 , 0…

如何用'-'解析字符串到节点js本地脚本? - python

我正在使用本地节点js脚本来处理字符串。我陷入了将'-'字符串解析为本地节点js脚本的问题。render.js:#! /usr/bin/env -S node -r esm let argv = require('yargs') .usage('$0 [string]') .argv; console.log(argv…

Python:传递记录器是个好主意吗? - python

我的Web服务器的API日志如下:started started succeeded failed 那是同时收到的两个请求。很难说哪一个成功或失败。为了彼此分离请求,我为每个请求创建了一个随机数,并将其用作记录器的名称logger = logging.getLogger(random_number) 日志变成[111] started [222] start…

Python-Excel导出 - python

我有以下代码:import pandas as pd import requests from bs4 import BeautifulSoup res = requests.get("https://www.bankier.pl/gielda/notowania/akcje") soup = BeautifulSoup(res.cont…