Skip to content

Binary operations between Numpy array and Pandas series convert to array dtype before executing #8674

Closed
@8one6

Description

@8one6

It looks like when you do a binary operation with a numpy array and a pandas series, the Pandas series is cast as the same type as the numpy array. In the case where the array is ints but the series is floats this can have odd results in Python 2.x with division. This was surprising to me because, element-wise, a float/int computation would upcast the denominator to a float. But not here in this vectorized case:

import numpy as np
import pandas as pd

j = np.array([0] * 5)
k = np.random.randn(5)

print j / k
print (j / pd.Series(k)).values
print (pd.Series(j) / k).values
print (pd.Series(j) / pd.Series(k)).values

####output####

[-0.  0. -0. -0.  0.]
[ inf  inf  inf  inf  inf]
[-0.  0. -0. -0.  0.]
[-0.  0. -0. -0.  0.]

So in the two "pure" cases (array and array, series and series) the ints are upcast to floats and we get 0's everywhere. In the 2nd mixed case (3rd case overall), everything gets converted to floats to match the array and everything goes fine. But in the 1st mixed case (2nd case overall) everything gets converted to ints to match the array and so you get what was, at least to me, an unexpected infinite result.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugDtype ConversionsUnexpected or buggy dtype conversionsNumeric OperationsArithmetic, Comparison, and Logical operations

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions