为什么大熊猫违反自己的警告?

时间:2018-11-01 18:19:03

标签: python pandas warnings

我有一些处理pandas DataFrames的代码。

运行此代码时,我得到SettingsWithCopyWarning个过多的消息。

所有警告均来自熊猫代码库本身。这让我感到困惑。

我已经更新了熊猫,所以我认为这不是版本问题。

我错误地使用了熊猫吗?每个人都收到这些警告吗?如果是这样,我应该关闭它们吗?我该怎么办?

这是我的程序运行时得到的输出示例(不会导致程序停止):

...
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexing.py:362: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  self.obj[key] = _infer_fill_value(value)
...
...
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexing.py:543: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  self.obj[item] = s
...

我假设pandas\core\indexing.py:543是指熊猫代码库中的一行违反了该准则并导致错误?这个假设正确吗?我认为这是因为它没有引用我的代码库中的任何行。

如果该假设不正确,我可以从我的代码库中提供代码行。

谢谢。

编辑:

我的熊猫版本:

>pip show pandas
Name: pandas
Version: 0.23.4

我的python版本:

>python
Python 3.6.4 |Anaconda custom (64-bit)| (default, Jan 16 2018, 10:22:32) [MSC v.1900 64 bit (AMD64)] on win32

下面的代码并不冗长,因为它又长又复杂,但是我认为我已经提出了一个典型的示例来说明可能的原因。

我的代码示例:

# I create a dask workflow and send a dataframe through it. 
def binning_workflow(df: pd.DataFrame, bins: dict) -> pd.DataFrame:
    workflow = {
    ...
    'bin_customs_osha': (bin_customs_data, 'bin_something_else',),
    ...
    }
    return dask.multiprocessing.get(workflow, 'merge_binned_columns_10')

# a function called during dask workflow 
def bin_customs_data(df):
    cols = ['OSHA_GRAVITY', 'OSHA_OPEN_DATE',]
    for v in cols:
        if v in df.columns:
            df.loc[:, v] = df[v].apply(lambda x: 'unknown' if x == -1 else x).astype(str)
    df.loc[:, 'OSHA_PENALTIES_IND_BIN'] = 0
    df.loc[df['NAICS_SECTOR_DESCRIPTION'] != 'Construction', 'OSHA_PENALTIES_IND_BIN'] = df['OSHA_PENALTIES_IND'].astype(str) + ' not const'
    return df

我只是感到奇怪,警告没有提及我的代码,我应该如何知道警告发生的地方?

0 个答案:

没有答案