Skip to content

Commit a29ad15

Browse files
vishwakftwproost
authored andcommitted
Move json_normalize to pd namespace (pandas-dev#27615)
1 parent 3359257 commit a29ad15

File tree

10 files changed

+37
-16
lines changed

10 files changed

+37
-16
lines changed

doc/redirects.csv

+1-1
Original file line numberDiff line numberDiff line change
@@ -777,7 +777,7 @@ generated/pandas.io.formats.style.Styler.to_excel,../reference/api/pandas.io.for
777777
generated/pandas.io.formats.style.Styler.use,../reference/api/pandas.io.formats.style.Styler.use
778778
generated/pandas.io.formats.style.Styler.where,../reference/api/pandas.io.formats.style.Styler.where
779779
generated/pandas.io.json.build_table_schema,../reference/api/pandas.io.json.build_table_schema
780-
generated/pandas.io.json.json_normalize,../reference/api/pandas.io.json.json_normalize
780+
generated/pandas.io.json.json_normalize,../reference/api/pandas.json_normalize
781781
generated/pandas.io.stata.StataReader.data_label,../reference/api/pandas.io.stata.StataReader.data_label
782782
generated/pandas.io.stata.StataReader.value_labels,../reference/api/pandas.io.stata.StataReader.value_labels
783783
generated/pandas.io.stata.StataReader.variable_labels,../reference/api/pandas.io.stata.StataReader.variable_labels

doc/source/reference/io.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,13 @@ JSON
5050
:toctree: api/
5151

5252
read_json
53+
json_normalize
5354

5455
.. currentmodule:: pandas.io.json
5556

5657
.. autosummary::
5758
:toctree: api/
5859

59-
json_normalize
6060
build_table_schema
6161

6262
.. currentmodule:: pandas

doc/source/user_guide/io.rst

+8-9
Original file line numberDiff line numberDiff line change
@@ -2136,27 +2136,26 @@ into a flat table.
21362136

21372137
.. ipython:: python
21382138
2139-
from pandas.io.json import json_normalize
21402139
data = [{'id': 1, 'name': {'first': 'Coleen', 'last': 'Volk'}},
21412140
{'name': {'given': 'Mose', 'family': 'Regner'}},
21422141
{'id': 2, 'name': 'Faye Raker'}]
2143-
json_normalize(data)
2142+
pd.json_normalize(data)
21442143
21452144
.. ipython:: python
21462145
21472146
data = [{'state': 'Florida',
21482147
'shortname': 'FL',
21492148
'info': {'governor': 'Rick Scott'},
2150-
'counties': [{'name': 'Dade', 'population': 12345},
2151-
{'name': 'Broward', 'population': 40000},
2152-
{'name': 'Palm Beach', 'population': 60000}]},
2149+
'county': [{'name': 'Dade', 'population': 12345},
2150+
{'name': 'Broward', 'population': 40000},
2151+
{'name': 'Palm Beach', 'population': 60000}]},
21532152
{'state': 'Ohio',
21542153
'shortname': 'OH',
21552154
'info': {'governor': 'John Kasich'},
2156-
'counties': [{'name': 'Summit', 'population': 1234},
2157-
{'name': 'Cuyahoga', 'population': 1337}]}]
2155+
'county': [{'name': 'Summit', 'population': 1234},
2156+
{'name': 'Cuyahoga', 'population': 1337}]}]
21582157
2159-
json_normalize(data, 'counties', ['state', 'shortname', ['info', 'governor']])
2158+
pd.json_normalize(data, 'county', ['state', 'shortname', ['info', 'governor']])
21602159
21612160
The max_level parameter provides more control over which level to end normalization.
21622161
With max_level=1 the following snippet normalizes until 1st nesting level of the provided dict.
@@ -2169,7 +2168,7 @@ With max_level=1 the following snippet normalizes until 1st nesting level of the
21692168
'Name': 'Name001'}},
21702169
'Image': {'a': 'b'}
21712170
}]
2172-
json_normalize(data, max_level=1)
2171+
pd.json_normalize(data, max_level=1)
21732172
21742173
.. _io.jsonl:
21752174

doc/source/whatsnew/v0.25.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ which level to end normalization (:issue:`23843`):
170170

171171
The repr now looks like this:
172172

173-
.. ipython:: python
173+
.. code-block:: ipython
174174
175175
from pandas.io.json import json_normalize
176176
data = [{

doc/source/whatsnew/v1.0.0.rst

100755100644
+3
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,9 @@ Deprecations
498498
- The parameter ``numeric_only`` of :meth:`Categorical.min` and :meth:`Categorical.max` is deprecated and replaced with ``skipna`` (:issue:`25303`)
499499
- The parameter ``label`` in :func:`lreshape` has been deprecated and will be removed in a future version (:issue:`29742`)
500500
- ``pandas.core.index`` has been deprecated and will be removed in a future version, the public classes are available in the top-level namespace (:issue:`19711`)
501+
- :func:`pandas.json_normalize` is now exposed in the top-level namespace.
502+
Usage of ``json_normalize`` as ``pandas.io.json.json_normalize`` is now deprecated and
503+
it is recommended to use ``json_normalize`` as :func:`pandas.json_normalize` instead (:issue:`27586`).
501504
-
502505

503506
.. _whatsnew_1000.prior_deprecations:

pandas/__init__.py

+2
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,8 @@
175175
read_spss,
176176
)
177177

178+
from pandas.io.json import _json_normalize as json_normalize
179+
178180
from pandas.util._tester import test
179181
import pandas.testing
180182
import pandas.arrays

pandas/io/json/__init__.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
from pandas.io.json._json import dumps, loads, read_json, to_json
2-
from pandas.io.json._normalize import json_normalize
2+
from pandas.io.json._normalize import _json_normalize, json_normalize
33
from pandas.io.json._table_schema import build_table_schema
44

55
__all__ = [
66
"dumps",
77
"loads",
88
"read_json",
99
"to_json",
10+
"_json_normalize",
1011
"json_normalize",
1112
"build_table_schema",
1213
]

pandas/io/json/_normalize.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import numpy as np
99

1010
from pandas._libs.writers import convert_json_to_lines
11+
from pandas.util._decorators import deprecate
1112

1213
from pandas import DataFrame
1314

@@ -108,7 +109,7 @@ def nested_to_record(
108109
return new_ds
109110

110111

111-
def json_normalize(
112+
def _json_normalize(
112113
data: Union[Dict, List[Dict]],
113114
record_path: Optional[Union[str, List]] = None,
114115
meta: Optional[Union[str, List]] = None,
@@ -332,3 +333,8 @@ def _recursive_extract(data, path, seen_meta, level=0):
332333
)
333334
result[k] = np.array(v, dtype=object).repeat(lengths)
334335
return result
336+
337+
338+
json_normalize = deprecate(
339+
"pandas.io.json.json_normalize", _json_normalize, "1.0.0", "pandas.json_normalize"
340+
)

pandas/tests/api/test_api.py

+4
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,9 @@ class TestPDApi(Base):
170170
"read_spss",
171171
]
172172

173+
# top-level json funcs
174+
funcs_json = ["json_normalize"]
175+
173176
# top-level to_* funcs
174177
funcs_to = ["to_datetime", "to_numeric", "to_pickle", "to_timedelta"]
175178

@@ -209,6 +212,7 @@ def test_api(self):
209212
+ self.funcs
210213
+ self.funcs_option
211214
+ self.funcs_read
215+
+ self.funcs_json
212216
+ self.funcs_to
213217
+ self.deprecated_funcs_in_future
214218
+ self.deprecated_funcs

pandas/tests/io/json/test_normalize.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33
import numpy as np
44
import pytest
55

6-
from pandas import DataFrame, Index
6+
from pandas import DataFrame, Index, json_normalize
77
import pandas.util.testing as tm
88

9-
from pandas.io.json import json_normalize
109
from pandas.io.json._normalize import nested_to_record
1110

1211

@@ -698,3 +697,10 @@ def test_with_large_max_level(self):
698697
]
699698
output = nested_to_record(input_data, max_level=max_level)
700699
assert output == expected
700+
701+
def test_deprecated_import(self):
702+
with tm.assert_produces_warning(FutureWarning):
703+
from pandas.io.json import json_normalize
704+
705+
recs = [{"a": 1, "b": 2, "c": 3}, {"a": 4, "b": 5, "c": 6}]
706+
json_normalize(recs)

0 commit comments

Comments
 (0)