ronnydw
ronnydw

Reputation: 953

Why different inferred dtype of pandas.Series with defined index?

Why do the following 2 statements generate a different dtype?

pd.Series({0: 1001, 1: 1002, 2: 1003})

0     1001
1     1002
2     1003
dtype: int64

pd.Series({0: 1001, 2: 1002, 4: 1003}, index=range(3))

0    1001.0
1       NaN
2    1002.0
dtype: float64

The second Series replaces the dict index by a new range. I don't see why it should generate a different dtype?

Upvotes: 0

Views: 40

Answers (1)

unutbu
unutbu

Reputation: 879729

NaN is used to represent missing data. NaN is also a floating-point value. So for a Series to contain NaN, it must have a dtype capable of holding floating-point values (such as float64 or object, but not int64).

Upvotes: 2

Related Questions