Katie C.
Katie C.

Reputation: 1

Why 'series' object is not callable?

I have an assignment that needs to assign df_confirmed_local to a DataFrame of confirmed cases that are local or epidemiologically linked with a local case, but when I run my code, it raises an error.

df_confirmed_local = df_covid.loc
[
(df_covid["Confirmed/probable"] == "Confirmed")
    & (df_covid["Case classification"] == 'Local case')
    (df_covid["Case classification"] == "Epidemiologically linked with local case")
]

df_confirmed_local

Upvotes: 0

Views: 369

Answers (1)

suvayu
suvayu

Reputation: 4664

Explanation:

As mentioned by the answer by @ArunJose, and the comment by @Minh-Long Luu, you have a boolean condition missing between the 2nd and 3rd condition, however it's an |.

(column1 == foo) & (column2 == bar) | (column2 == baz)

Each column is a pandas.Series object. When used in a boolean expression, it also returns a Series of booleans. If you miss one of the conditions (|) it looks like a function call to Python, leading to the "object is not callable" error:

(column2 == bar) (column2 == baz)  # looks like pd.Series()

That said, you probably should group your conditions with parentheses to reflect the correct precedence:

(column1 == foo) & ((column2 == bar) | (column2 == baz))

Something like this:

df_confirmed_local = df_covid.loc[
    (df_covid["Confirmed/probable"] == "Confirmed")
    & (
        (df_covid["Case classification"] == "Local case")
        | (
            df_covid["Case classification"]
            == "Epidemiologically linked with local case"
        )
    )
]

Upvotes: 2

Related Questions