Comments (3)
what's even weirder with to_dict is that pd.NA gets converted to None, whereas np.nan and None are both converted to nan
None
doesn't get converted to nan
in the to_json
step. Rather, it happens in the dataframe constructor. If you craft a dataframe that explicitly hasNone
, you'll see that it does return a None
.
In [37]: pd.DataFrame({'a': [1, None, pd.NaT, np.nan, pd.NA]}, dtype='object').to_dict(orient='records')
Out[37]: [{'a': 1}, {'a': None}, {'a': NaT}, {'a': nan}, {'a': None}]
from pandas.
I agree things should be consistent. what's even weirder with to_dict
is that pd.NA
gets converted to None
, whereas np.nan
and None
are both converted to nan
.
from pandas.
I'm seeing a lot of inconsistencies here, and they don't all appear to be pandas related.
Is this a bug, or intentional for some reason?
I'm suspecting this is intentional because data[0]['b']
is numerical, data[1]['b']
as None
is treated as numerical or NaN
.
With all the json conversions as well, I wouldn't expect, NaN
, Null
, or None
to be maintained.
While it would be ideal for them to match, I'm beginning to thing that won't be possible.
Test:
import pandas as pd
import json
data = [{'a': 1, 'b': 1}, {'a': 2, 'b': None}]
df = pd.DataFrame(
data
)
print("input data")
print(data)
print("\nafter conversion to dataframe")
print(df)
print("\nafter conversion to_dict")
print(df.to_dict(orient='records'))
print("\nafter conversion to_json")
print(df.to_json(orient='records'))
print("\nafter python json processing")
print(json.loads(df.to_json(orient='records')))
output:
input data
[{'a': 1, 'b': 1}, {'a': 2, 'b': None}]
after conversion to dataframe
a b
0 1 1.0
1 2 NaN
after conversion to_dict
[{'a': 1, 'b': 1.0}, {'a': 2, 'b': nan}]
after conversion to_json
[{"a":1,"b":1.0},{"a":2,"b":null}]
after python json processing
[{'a': 1, 'b': 1.0}, {'a': 2, 'b': None}]
Maybe NaN
in Pandas should convert to None
with to_dict
instead of nan
?
from pandas.
Related Issues (20)
- BUG: FutureWarning appears when assigning a bool list through .at HOT 6
- BUG: how to convert .xls to .xlsx because Pandas failed to open .xls files HOT 13
- BUG: concatenating non overlapping time series with non ns unit leads to dataframe with missing data HOT 1
- ENH: Support μs Greek small letter mu
- BUG: Unexpected KeyError in `transform()` with dict-like func argument HOT 3
- ENH: Introduce type-safe constructors for `Timestamp` and `Timedelta`. HOT 2
- BUG: pandas.api.types.is_integer_dtype evaluates 'P' and 'p' as integers HOT 1
- BUG: DataFrame's loc set works incorrectly with scalar column key and single value DataFrame item
- BUG: DataFrame `convert_dtype='pyarrow'` with int64_t limits throws Warning
- DOC: delim_whitespace deprecation warning advocates code that generates a warning in Python 3.12 HOT 2
- DOC: User guide still recommends delim_whitespace HOT 3
- BUG: Add pyarrow strings to any_string_dtype fixture
- BUG: strings fails to convert to nullable Integer dtype in dataframe/series HOT 2
- DOC: Enforce Numpy Docstring Validation | pandas.Int16Dtype through pandas.IntervalIndex HOT 3
- BUG: KeyError: 'Step Nr.s' HOT 5
- DISC: Supporting numpy StringDType in Pandas HOT 7
- DOC: Enforce Numpy Docstring Validation | pandas.Series HOT 10
- DOC: Enforce Numpy Docstring Validation | pandas.Timestamp HOT 4
- DOC: `pd.concat` description of ordering when passing a mapping is unclear HOT 2
- TST: mutation testing results (possible tests to add)
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.