【问题标题】:Pandas IndexingError - Unalignable boolean Series provided as indexer (index of the boolean Series and of the indexed object do not match)Pandas IndexingError - 作为索引器提供的不可对齐的布尔系列(布尔系列的索引和索引对象的索引不匹配)
【发布时间】:2022-01-24 02:35:06
【问题描述】:

背景:我有一个函数调用一个名为df 的熊猫数据框。在df 变成exceptions_report(仍然是pandas 数据框)之前,我对它进行了各种操作和清理。

问题: 在我的代码中,我试图删除不符合 2x 标准的行。我的整个程序似乎抛出了以下IndexingError-

<ipython-input-725-33f542debed3>:7: FutureWarning: The default value of regex will change from True to False in a future version.
  df.columns = df.columns.str.replace(r'columns.', '')
<ipython-input-726-412358be9bd2>:51: UserWarning: Boolean Series key will be reindexed to match DataFrame index.
  df = df[~mask2].drop(columns="value")
---------------------------------------------------------------------------
IndexingError                             Traceback (most recent call last)
<ipython-input-727-b420b41509b7> in <module>
     36     print("-----------------------------\n","EXCEPTION REPORT:", excep_row_count, "rows", "\n-----------------------------")
     37 
---> 38 xlsx_writer()

<ipython-input-727-b420b41509b7> in xlsx_writer()
      1 # Function that writes Exceptions Report and API Response as a consolidated .xlsx file.
      2 def xlsx_writer():
----> 3     df_raw, exceptions_df = ownership_qc()
      4 
      5 #   Creating and defining filename for exceptions report

<ipython-input-726-412358be9bd2> in ownership_qc()
     49     # Remove lines that match all conditions
     50     mask2 = (~exceptions_df["value"].isna() & (exceptions_df["Entity ID %"] == exceptions_df["value"]) & (exceptions_df["Account # %"] == exceptions_df["value"]))
---> 51     df = df[~mask2].drop(columns="value")
     52 
     53     return df_raw, exceptions_df

~\Anaconda3\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
   3013         # Do we have a (boolean) 1d indexer?
   3014         if com.is_bool_indexer(key):
-> 3015             return self._getitem_bool_array(key)
   3016 
   3017         # We are left with two options: a single key, and a collection of keys,

~\Anaconda3\lib\site-packages\pandas\core\frame.py in _getitem_bool_array(self, key)
   3066         # check_bool_indexer will throw exception if Series key cannot
   3067         # be reindexed to match DataFrame rows
-> 3068         key = check_bool_indexer(self.index, key)
   3069         indexer = key.nonzero()[0]
   3070         return self._take_with_is_copy(indexer, axis=0)

~\Anaconda3\lib\site-packages\pandas\core\indexing.py in check_bool_indexer(index, key)
   2267         mask = isna(result._values)
   2268         if mask.any():
-> 2269             raise IndexingError(
   2270                 "Unalignable boolean Series provided as "
   2271                 "indexer (index of the boolean Series and of "

IndexingError: Unalignable boolean Series provided as indexer (index of the boolean Series and of the indexed object do not match).

如您所见,针对此错误引用的代码被编写为仅从 exceptions_df 中删除行确实符合 2x 标准-

  1. 所有权审核说明⊃(包含)“已审核”
  2. 所有权审核说明包含一个 xx.xx% 值,即 == Entity ID %Account # % 列。

错误代码的函数: 以下函数包含返回IndexingError 的代码。该函数的目标是在返回之前对df(然后变为exceptions_df)进行操作,准备在另一个函数中写入.xlsx 文件。

# Function to compute ownership exceptions
def ownership_qc():

#   Calling function that returns dataframe
    df = unpack_response()

#   Making a copy of df (normalized API response) to be written by def_xlsx_writer() function to same .xslsx as exceptions_df
    df_raw = df.copy()
    
#   Set Holding Account Number column values as dashes, if empty. Note: required to ensure Ownership Calculations work correctly.
    df.loc[df["Holding Account Number"].isnull(),'Holding Account Number'] = "-"
    
#   Setting % Ownership column as a percentage.
    df["% Ownership"] = 100 * df["% Ownership"].round(2)

#   Setting QC Column values, ready for output calculations
    df['Entity ID %'] = '0.00'
    df['Account # %'] = '0.00'
    
#   Changing float64 columns to strings, for referencing purposes
    df['Ownership Audit Note'] = df['Ownership Audit Note'].astype(str)

#   Ownership Calculations
    df['Entity ID %'] = df.groupby('Entity ID')['% Ownership'].transform(sum).round(2)
    df['Account # %'] = df.groupby('Holding Account Number')['% Ownership'].transform(sum).round(2)   
    
#   Dropping obsolete columns
    exceptions_df = df.drop(['Model Type', 'Valuation (USD)', 'Top Level Legal Entity', 'Financial Service', 'Account Close Date'], axis=1)
    
#   Dropping any 'Direct Owned' rows
    exceptions_df = exceptions_df[(~(exceptions_df['Holding Account'].str.contains('Directly Owned')))]

#   Scenario 1 - If 'Ownership Audit Note' contains "Reviewed" and 'Ownership Audit Note' contains a percentage value (xx.xx%) which is also == 'Entity ID %' and 'Account # %' then drop row.
    # Remove lines that match all conditions
    mask1 = exceptions_df["Ownership Audit Note"].str.lower().str.contains("Reviewed")
    exceptions_df["value"] = exceptions_df.loc[mask1, "Ownership Audit Note"].str.extract(pat=r"\[\D*(\d+\.?\d*)%\]")
    exceptions_df["value"] = exceptions_df["value"].astype("float")
    
    # Remove lines that match all conditions
    mask2 = (~exceptions_df["value"].isna() & (exceptions_df["Entity ID %"] == exceptions_df["value"]) & (exceptions_df["Account # %"] == exceptions_df["value"]))
    # THE BELOW CODE SEGMENT IS THROWING BACK THE IndexingError exception.
    df = df[~mask2].drop(columns="value")
                                  
    return df_raw, exceptions_df

注意事项:我已经尝试并能够使用更小的df 运行问题代码,因此我知道此函数中的其他原因导致了问题。 p>

有人对我哪里出错有任何建议或提示吗?

【问题讨论】:

    标签: python pandas index-error


    【解决方案1】:

    我仍然不确定发生这种情况的确切原因,但是通过使用新名称 (exceptions_df) 保存 df,我的代码运行良好 -

    mask2 = (~df["value"].isna() & (df["Entity ID %"] == df["value"]) & (df["Account # %"] == df["value"]))
    exceptions_df = df[~mask2].drop(columns="value")
    

    【讨论】:

      猜你喜欢
      • 2020-08-05
      • 2022-12-14
      • 2023-02-03
      • 2020-12-15
      • 1970-01-01
      • 2021-04-20
      • 2018-01-03
      • 1970-01-01
      • 2020-01-03
      相关资源
      最近更新 更多