Comments (7)
Note that the behavior described above is different if the format is JSON-Lines and the "pyarrow" engine is used:
json_lines = b'{"col1": 1, "col2": 1.0}\n{"col1": 2, "col2": 2.0}'
df = pd.read_json(io.BytesIO(json_lines), lines=True, engine="pyarrow")
assert not (df["col1"].dtype == df["col2"].dtype)
On the other hand, the downcasting appears again if the "ujson" engine (the default one) is used:
json_lines = b'{"col1": 1, "col2": 1.0}\n{"col1": 2, "col2": 2.0}'
df = pd.read_json(io.BytesIO(json_lines), lines=True)
assert df["col1"].dtype == df["col2"].dtype
from pandas.
Thats a good point
from pandas.
Also note that this downcasting is not performed by pandas.read_csv
:
csv_content = "col1,col2\n1,1.0\n2,2.0"
df = pd.read_csv(io.StringIO(csv_content))
assert not (df["col1"].dtype == df["col2"].dtype)
from pandas.
Additionally, str column is also cast to int:
d = [{"col1": 1, "col2": 1.0, "col3": "1"}, {"col1": 2, "col2": 2.0, "col3": "2"}]
df = pd.read_json(io.StringIO(json.dumps(d)))
assert df["col1"].dtype == df["col2"].dtype == df["col3"].dtype
from pandas.
Passing dtype=False
, I get the expected behavior of the OP. But the docstring doesn't seem clear to me:
If True, infer dtypes; if a dict of column to dtype, then use those; if False, then donโt infer dtypes at all, applies only to the data.
Perhaps the language can be improved.
@albertvillanova - can you confirm if dtype=False
satisfies your use-case? Labeling this as just a docs issue for now.
from pandas.
@rhshadrach thanks for your reply.
Unfortunately, passing dtype=False
does not satisfy my use-case, because indeed I was passing dtype_backend="pyarrow"
as well (I did not mention it in the description to make things simpler).
Therefore the float-to-int downcasting persists even if passing dtype=False
when passing dtype_backend="pyarrow"
:
d = [{"col1": 1, "col2": 1.0}, {"col1": 2, "col2": 2.0}]
df = pd.read_json(io.StringIO(json.dumps(d)), dtype_backend="pyarrow")
assert df["col1"].dtype == df["col2"].dtype
df = pd.read_json(io.StringIO(json.dumps(d)), dtype_backend="pyarrow", dtype=False)
assert df["col1"].dtype == df["col2"].dtype
Additionally, I would like to ask if in the former case (when no passing dtype_backend="pyarrow"
), there would be other side effects when passing dtype=False
. Would other dtypes be treated differently?
from pandas.
Thanks @albertvillanova - I've reclassified this issue.
from pandas.
Related Issues (20)
- BUG: datetime64[s] data changes when put into HDFStore HOT 1
- BUG: random crash / hang when calculating rolling sum HOT 2
- BUG: 0/0 with arrow backend is not "NA" HOT 3
- BUG: pivot_table chokes on pd.DatetimeTZDtype if there are no rows.
- DOC: "Accelerated operations" talks about speedup in obsolete versions of Pandas HOT 2
- BUG: rounding datetime in series is broken HOT 1
- BUG: DataFrame to JSON failed when it with UUID HOT 4
- ENH: merge_asof with "howโ functionality
- ENH: is it possible to save a reference to a method depending on the version HOT 3
- ValueError: Usecols do not match columns, columns expected but not found: ['Col3', 'Col1'] HOT 6
- ENH: Query function to have a star * for all columns
- BUG: read_html returns empty list HOT 3
- database is locked
- BUG: Enlarging multilevel index fails if one or more level keys are None HOT 2
- BUG: `.dt.microsecond` for pyarrow-backed Series returns 0
- VOTE: Voting issue for PDEP-14: Dedicated string data type for pandas 3.0 HOT 10
- DEPR: `keep_default_dates` and `convert_dates` in `read_json`
- ENH: Add Option to Include Array Offset as MultiIndex Level in `explode()` HOT 5
- DOC: Sort the pandas API reference navbar in alphabetical order HOT 1
- ENH: In pd.cut(), allow bins='auto' (leveraging `np.histogram_bin_edges`)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pandas.