import numpy as np import pandas as pd vals = [1.5, 2.5] pd.Series(vals, dtype=np.int64) # <- float64 pd.Series(np.array(vals), dtype=np.int64) # <- float64 pd.Series(np.array(vals, dtype=object), dtype=np.int64) # <- raises pd.Series(vals).astype(np.int64) # <- int64 pd.Index(vals, dtype=np.int64) # <- raises pd.Index(np.array(vals), dtype=np.int64) # <- float64 pd.Index(np.array(vals, dtype=object), dtype=np.int64) # <- raises pd.Index(vals).astype(np.int64) # <- int64
xref #49372
When passing non-round float-like data and an integer dtype we have special logic
# TODO: where is the discussion that documents the reason for this?that ignores the dtype keyword and silently returns a float dtype. This violates at least 3 aspirational rules of thumb :
pd.Series(values, dtype=dtype)
should match pd.Series(values).astype(dtype)
I would be OK with either returning integer dtype or raising here. If we prioritize consistency with .astype behavior then this will depend on decisions in #45588.
Currently we have 13 tests that fail if we disable this behavior (9 of which are a single parametrized test) and they are all directly testing this behavior, so changing it wouldn't be world-breaking.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4