0

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?

Alex Metsai
  • 1,837
  • 5
  • 12
  • 24
Manu Chadha
  • 15,555
  • 19
  • 91
  • 184

3 Answers3

0

pandas uses array-broadcasting similar to numpy... as such, operations are performed on each element rather than on the object itself. (see https://stackoverflow.com/a/29955358/9703451 for example)

what do you try to accomplish with your query?

raphael
  • 2,159
  • 17
  • 20
0

It may have overloaded __eq__ operator.

See this example

class A:
    def __eq__(self, o: object) -> bool:
        if isinstance(o, str):
            return 13
        return super().__eq__(o)


a = A()

# returns False
print(a == 2)

# returns 13 because of overriden __eq__ in A class
print(a == 's')

More info: Python docs.

Adam Tokarski
  • 659
  • 4
  • 13
0

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.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252