[PYTHON] What to do when Unalignable boolean Series provided as indexer

Event: I was angry when I tried to extract data with a non-NaN index with notna

--Environment --macOS Catalina version 10.15.7 - Python 3.8.5 - pandas 1.1.3

import pandas

if __name__ == '__main__':
    df = pandas.read_csv('CSV.csv')
    print(df[df.query('name== "Ponsuke"')['start'].notna()])

Error message


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

Cause unknown

Because the index of the index of the DataFrame you are trying to extract is not in the result of notna? I want someone to tell me.

#The index of the DataFrame you are trying to extract is 0~There are 40 up to 39
print(df)
#Name Count Start End
#0 Ponsuke 1 9:00  18:00
# ...abridgement...
#39 Ponsuke 10 NaN NaN

#The index of the result of notna is full of missing numbers and only 10
print(df.query('name== "Ponsuke"')['start'].notna())
# 0      True
# 2      True
# 5      True
# 11     True
# 14    False
# 21     True
# 24     True
# 29    False
# 34     True
# 39    False
# Name:start, dtype: bool

If you extract from a DataFrame that matches the index of the result of notna, it seems quite so

#DataFrame that fits the index of the result of notna
print(df.query('name== "Ponsuke"'))
#Name Count Start End
#0 Ponsuke 1 9:00  18:00
#2 Ponsuke 3 9:00  13:00
#5 Ponsuke 7 12:00    NaN
#11 Ponsuke 5 9:00    NaN
#14 Ponsuke 4 NaN NaN
#21 Ponsuke 2 18:00    NaN
#24 Ponsuke 6 18:00    NaN
#29 Ponsuke 9 NaN 18:00
#34 Ponsuke 8 12:00    NaN
#39 Ponsuke 10 NaN NaN

Action: Extract from DataFrame that matches the index of the result of notna

import pandas

if __name__ == '__main__':
    df = pandas.read_csv('CSV.csv')
    ponsuke = df.query('name== "Ponsuke"')
    print(ponsuke[ponsuke['start'].notna()])

did it


Name Count Start End
0 Ponsuke 1 9:00  18:00
2 Ponsuke 3 9:00  13:00
5 Ponsuke 7 12:00    NaN
11 Ponsuke 5 9:00    NaN
21 Ponsuke 2 18:00    NaN
24 Ponsuke 6 18:00    NaN
34 Ponsuke 8 12:00    NaN

Recommended Posts

What to do when Unalignable boolean Series provided as indexer
What to do when Ubuntu crashes
What to do when PermissionError of tempfile.mkstemp occurs
curl: (60) What to do when Issuer certificate is invalid.
What to do when gdal_merge creates a huge file
What to do when raise ValueError, "unsupported hash type"
What to do when "cannot import name xxx" [Python]
What to do when you can't bind CaboCha to Python
[AWS] What to do when you want to pip with Lambda
What to do when PyCharm font is strange or garbled
What to do when a Remove Error occurs when updating conda
What to do when an error occurs with import _ssl
Be careful when assigning Series as a column to pandas.DataFrame
What to do if package installation fails when deploying to heroku
What to do when "SSL: CERTIFICATE_VERIFY_FAILED _ssl.c: 1056" appears in Python
OSError: [Errno 40] What to do when Message too long appears
What to do when "Invalid HTTP_HOST header" appears in Django
What to do if you can't log in as root
ImportError: No module What to do when you are told
[AWS] What to do when the ping command causes a "timeout"
[Beanstalk] What to do when an error occurs with import uuid
What to do when the jupyterlab extension settings are not reflected
What to do if Insecure Platform Warning appears when running Python
What to do when you get "I can't see the site !!!!"
What to do when UnicodeDecodeError occurs during read_csv in pandas (pd.read_table ())
What to do when ModuleNotFoundError: No module named'XXX' occurs in Python
What to do when the value type is ambiguous in Python?
What to do if yum breaks
What to do with Magics install
What to do with PYTHON release?
What to do to get tensorflow-gpu to work
[Memorandum] What to do when a warning appears after executing pip list
What to do when the result downloaded via scrapy is in English
What to do when pyinstaller: error: argument --add-binary: invalid add_data_or_binary value: appears
[Python] What to do when an error related to SSL authentication is returned
What to do if an error occurs when importing numpy with VScode
What to do if you get an error when trying to load mnist
What to do if fprintd requires a password when registering your fingerprint
What to do when the warning "The environment is in consistent ..." appears in the Anaconda environment
Notes on what to do when matplotlib scatter () / scatter3d () does not work
What to do if you get an error when installing Dlib (Ubuntu)
[OSX] [pyenv] What to do when an SSL error occurs in pip
What to do when psycopg2 throws an error when pipenv lock under Pipenv environment
What to do when a warning message is displayed in pip list
What I referred to when studying tkinter
What to do after installing Linux (Ubuntu)
Let's summarize what you want to do.
What to do if CERTIFICATE_VERIFY_FAILED occurs when nltk.download () is done on macOS pyhon
[python] What to do when an error occurs in send_keys of headless chrome
What to do when a warning appears around Python integration in Neovim's CheckHealth
What to do when xlrd.biffh.XLRDError: Unsupported format, or corrupt file: Expected BOF record;
[Ubuntu 18.04 LTS] What to do when the screen resolution cannot be selected [NVIDIA]
What to do when "TypeError: data type not understood" appears in python's numpy.zeros
What to do if you get an error when installing python with pyenv
What to do if abort is displayed when inputting camera video in OpenCV
What to do when [Errno 2] No such file or directory appears in Python
What to do when the graph does not appear in jupyter (ipython) notebook
[EC2] What to do when selenium is stuck and processing does not proceed
What to do when you want to receive files from a Windows client remotely