title: Pandas IndexingError - Unalignable boolean Series provided as indexer (index of the boolean Series and of the indexed object do not match)
body: <p><strong>Background:</strong> I have a function that calls a pandas dataframe called <code>df</code>. I do various manipulation and clean-up on the <code>df</code> before it becomes <code>exceptions_report</code> (still a pandas dataframe).</p>
  <p><strong>Issue:</strong> In my code, I am trying to drop rows that don't meet 2x criteria. My whole program seems to throw back the following <code>IndexingError</code>-</p>
  <pre><code>&lt;ipython-input-725-33f542debed3&gt;: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.', '')
  &lt;ipython-input-726-412358be9bd2&gt;:51: UserWarning: Boolean Series key will be reindexed to match DataFrame index.
    df = df[~mask2].drop(columns=&quot;value&quot;)
  ---------------------------------------------------------------------------
  IndexingError                             Traceback (most recent call last)
  &lt;ipython-input-727-b420b41509b7&gt; in &lt;module&gt;
       36     print(&quot;-----------------------------\n&quot;,&quot;EXCEPTION REPORT:&quot;, excep_row_count, &quot;rows&quot;, &quot;\n-----------------------------&quot;)
       37 
  ---&gt; 38 xlsx_writer()
  
  &lt;ipython-input-727-b420b41509b7&gt; in xlsx_writer()
        1 # Function that writes Exceptions Report and API Response as a consolidated .xlsx file.
        2 def xlsx_writer():
  ----&gt; 3     df_raw, exceptions_df = ownership_qc()
        4 
        5 #   Creating and defining filename for exceptions report
  
  &lt;ipython-input-726-412358be9bd2&gt; in ownership_qc()
       49     # Remove lines that match all conditions
       50     mask2 = (~exceptions_df[&quot;value&quot;].isna() &amp; (exceptions_df[&quot;Entity ID %&quot;] == exceptions_df[&quot;value&quot;]) &amp; (exceptions_df[&quot;Account # %&quot;] == exceptions_df[&quot;value&quot;]))
  ---&gt; 51     df = df[~mask2].drop(columns=&quot;value&quot;)
       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):
  -&gt; 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
  -&gt; 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():
  -&gt; 2269             raise IndexingError(
     2270                 &quot;Unalignable boolean Series provided as &quot;
     2271                 &quot;indexer (index of the boolean Series and of &quot;
  
  IndexingError: Unalignable boolean Series provided as indexer (index of the boolean Series and of the indexed object do not match).
  </code></pre>
  <p>As you can see, the code which is being referenced for this error was written to only drop rows from <code>exceptions_df</code> <strong>which do meet 2x criteria</strong>-</p>
  <ol>
  <li>Ownership Audit Note ⊃ (contains) &quot;Reviewed&quot;</li>
  <li>Ownership Audit Note contains a xx.xx% value that is == <code>Entity ID %</code> and <code>Account # %</code> columns.</li>
  </ol>
  <p><strong>Function with the bad code:</strong> The following function contains the code that is throwing back the <code>IndexingError</code>. This objective of this function is to perform manipulation of <code>df</code> (which then becomes <code>exceptions_df</code>) before returning it, ready for writing to an .xlsx file in another function.</p>
  <pre><code># 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[&quot;Holding Account Number&quot;].isnull(),'Holding Account Number'] = &quot;-&quot;
      
  #   Setting % Ownership column as a percentage.
      df[&quot;% Ownership&quot;] = 100 * df[&quot;% Ownership&quot;].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 &quot;Reviewed&quot; 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[&quot;Ownership Audit Note&quot;].str.lower().str.contains(&quot;Reviewed&quot;)
      exceptions_df[&quot;value&quot;] = exceptions_df.loc[mask1, &quot;Ownership Audit Note&quot;].str.extract(pat=r&quot;\[\D*(\d+\.?\d*)%\]&quot;)
      exceptions_df[&quot;value&quot;] = exceptions_df[&quot;value&quot;].astype(&quot;float&quot;)
      
      # Remove lines that match all conditions
      mask2 = (~exceptions_df[&quot;value&quot;].isna() &amp; (exceptions_df[&quot;Entity ID %&quot;] == exceptions_df[&quot;value&quot;]) &amp; (exceptions_df[&quot;Account # %&quot;] == exceptions_df[&quot;value&quot;]))
      # THE BELOW CODE SEGMENT IS THROWING BACK THE IndexingError exception.
      df = df[~mask2].drop(columns=&quot;value&quot;)
                                    
      return df_raw, exceptions_df
  </code></pre>
  <p><strong>Things to note:</strong> I have tried and been able to run the problem piece of code with a much smaller <code>df</code>, so I know something else in this function is causing the issue.</p>
  <p>Does anyone have any suggestions or hints as to where I have gone wrong?</p>