During debugging, pandas gave an error: "TypeError: 'NoneType' object is not callable"

During debugging, pandas gave an error: "TypeError: 'NoneType' object is not callable"

我有记录table。

   data  stage  epoch
0     0  train      0
1     1  valid      1
2     2  train      0
3     3  valid      1
4     4  train      2
5     5  valid      3

我想通过“train and ”valid“从”epoch“的最后一个 0 开始将这个 table 分开。我的代码如下:

import numpy as np
import pandas as pd    

class SL(object):

    def select(self, df):
        df_train = df[df["stage"] == "train"]
        df_valid = df[df["stage"] == "valid"]

        index_zero = np.where(df["epoch"].values == 0)[0][-1]
        df_train = df_train.loc[index_zero:, :]
        df_valid = df_valid.loc[index_zero:, :]
        print(df_train,"\n", df_valid)

df = pd.DataFrame({"data":range(6), "stage":["train","valid","train", "valid","train","valid"], "epoch":[0,1,0,1,2,3]})
SL().select(df)

当我直接运行时,它工作正常,

 data  stage  epoch
2     2  train      0
4     4  train      2 

    data  stage  epoch
3     3  valid      1
5     5  valid      3

但是当我用Pycharm调试时,df_valid = df_valid.loc[index_zero:, :]总是报错TypeError: 'NoneType' object is not callable,有谁知道为什么吗?

IIUC,可以先过滤掉最后一个0之前的行,然后用groupby:

拆分
s = df['epoch'].eq(0).cumsum()
d = {k: g for k,g in df[s.eq(s.iloc[-1])].groupby(df['stage'])}

输出:

{'train':    data  stage  epoch
 2     2  train      0
 4     4  train      2,
 'valid':    data  stage  epoch
 3     3  valid      1
 5     5  valid      3}

这是一个已知错误,仅在 Python 3.10 上调试某些 Numpy-backed 代码时出现。该错误源自 Cython,最近已修复。几天前 Numpy 1.22.4 已经发布,使用新的 Cython 构建,也解决了 Numpy 中的问题。现在您可能仍然需要重建 Pandas 和 Scikit-learn 才能使用最新的 Numpy。

您可以使用类似于以下的命令来执行此操作:

CFLAGS="-DCYTHON_FAST_PYCALL=0" pip install --force-reinstall --no-binary numpy,scikit-learn,pandas scikit-learn pandas numpy scipy