Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to prevent: "UserWarning: Boolean Series key will be reindexed to match DataFrame index"

I am having some issues with my Python code. When working with DataFrames I get a UserWarning and I’m not really sure of how to prevent it.

for index in matplatsID:
      mask = (kameraData["Tid"].dt.hour >= timme) & (kameraData["Tid"].dt.hour < timme+1)
      matplatsSummaHastigheter += kameraData[mask][kameraData["MätplatsID"] == index]["Hastighet"].sum()
      matplatsAntalFordon += kameraData[mask][kameraData["MätplatsID"] == index]["Hastighet"].count()
  
UserWarning: Boolean Series key will be reindexed to match DataFrame index.
  matplatsSummaHastigheter += kameraData[mask][kameraData["MätplatsID"] == index]["Hastighet"].sum()
UserWarning: Boolean Series key will be reindexed to match DataFrame index.
  matplatsAntalFordon += kameraData[mask][kameraData["MätplatsID"] == index]["Hastighet"].count()

It generates these warnings, and I am dumbfounded of how to avoid them, any ideas of what to do?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You need chain all 3 conditions for testing original DataFrame:

for index in matplatsID:
     mask = (kameraData["Tid"].dt.hour >= timme) & 
            (kameraData["Tid"].dt.hour < timme+1) & 
            (kameraData["MätplatsID"] == index)
     matplatsSummaHastigheter += kameraData.loc[mask, "Hastighet"].sum()
     matplatsAntalFordon += kameraData.loc[mask, "Hastighet"].count()

In your solution first filter by mask and then by kameraData["MätplatsID"] == index, so raise error, because filter of filtered DataFrame.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading