Skip to content
Advertisement

How does `s = (iowa_file_data.dtypes == ‘object’)` make `s` a variable of type `Series`

I wrote (copied) a code

iowa_file_data = pd.read_csv(iowa_train_prices_file_path)
    #dtypes is a Series
    s = (iowa_file_data.dtypes == 'object')

I get that iowa_file_data is of type dataframe and dtypes is a Series. But how did s become a Series? Does output of (iowa_file_data.dtypes == 'object') make a Series?

Advertisement

Answer

Pandas (and the underlying numpy) use special overloads of __eq__ operators. The goal is that most operation between numpy arrays, dataframes or series still return an object of same type. And when you have one Series on one side of the operator and a scalar on the other side, the scalar is broadcasted over the missing dimension to build a Series of same size (and index) as the initial series, where all cells have the value (and the type) of the scalar.

So yes, if ser is a Series, ser == 'object' is also a Series of same size and index as ser.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement