From bed97724ea4b7e5161e636e61cd417db4e78d8f4 Mon Sep 17 00:00:00 2001 From: Irv Lustig Date: Wed, 15 Jul 2020 13:28:00 -0400 Subject: [PATCH 1/8] Add orient=tight format for dictionaries --- doc/source/whatsnew/v1.1.0.rst | 17 +++++ pandas/core/frame.py | 72 ++++++++++++++++++++-- pandas/tests/frame/methods/test_to_dict.py | 28 ++++++++- 3 files changed, 111 insertions(+), 6 deletions(-) diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index 814dbe999d5c1..e9d9d75e9a62f 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -271,6 +271,23 @@ change, as ``fsspec`` will still bring in the same packages as before. .. _fsspec docs: https://filesystem-spec.readthedocs.io/en/latest/ +.. _whatsnew_110.dict_tight: + +DataFrame.from_dict and DataFrame.to_dict have new ``'tight'`` option +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +A new ``'tight'`` dictionary format that preserves :class:`MultiIndex` entries and names +is now available, and can be used with the standard `json` library to produce a tight +representation of :class:`DataFrame` objects (:issue:`4889`). + +.. ipython:: python + + df = pd.DataFrame.from_records([[1, 3], [2, 4]], + index=pd.MultiIndex.from_tuples([("a", "b"), ("a", "c")], names=["n1", "n2"]), + columns=pd.MultiIndex.from_tuples([("x", 1), ("y", 2)], names=["z1", "z2"])) + df + df.to_dict(orient='tight') + .. _whatsnew_110.enhancements.other: Other enhancements diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 0268d19e00b97..a6922062cac55 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -1236,15 +1236,21 @@ def from_dict(cls, data, orient="columns", dtype=None, columns=None) -> "DataFra ---------- data : dict Of the form {field : array-like} or {field : dict}. - orient : {'columns', 'index'}, default 'columns' + orient : {'columns', 'index', 'tight'}, default 'columns' The "orientation" of the data. If the keys of the passed dict should be the columns of the resulting DataFrame, pass 'columns' (default). Otherwise if the keys should be rows, pass 'index'. + If 'tight', assume a dict with keys ['index', 'columns', 'data', + 'index_names', 'column_names'] + + .. versionadded:: 1.1.0 + 'tight' as an allowed value for the ``orient`` argument + dtype : dtype, default None Data type to force, otherwise infer. columns : list, default None Column labels to use when ``orient='index'``. Raises a ValueError - if used with ``orient='columns'``. + if used with ``orient='columns'`` or ``orient='tight'``. .. versionadded:: 0.23.0 @@ -1257,6 +1263,7 @@ def from_dict(cls, data, orient="columns", dtype=None, columns=None) -> "DataFra DataFrame.from_records : DataFrame from structured ndarray, sequence of tuples or dicts, or DataFrame. DataFrame : DataFrame object creation using constructor. + DataFrame.to_dict : Convert the DataFrame to a dictionary. Examples -------- @@ -1279,6 +1286,20 @@ def from_dict(cls, data, orient="columns", dtype=None, columns=None) -> "DataFra row_1 3 2 1 0 row_2 a b c d + Specify ``orient='tight'`` to create the DataFrame using a 'tight' + format. + >>> data = {'index': [('a', 'b'), ('a', 'c')], + 'columns': [('x', 1), ('y', 2)], + 'data': [[1, 3], [2, 4]], + 'index_names': ['n1', 'n2'], + 'column_names': ['z1', 'z2']} + >>> pd.DataFrame.from_dict(data, orient='tight') + z1 x y + z2 1 2 + n1 n2 + a b 1 3 + c 2 4 + When using the 'index' orientation, the column names can be specified manually: @@ -1297,13 +1318,27 @@ def from_dict(cls, data, orient="columns", dtype=None, columns=None) -> "DataFra data = _from_nested_dict(data) else: data, index = list(data.values()), list(data.keys()) - elif orient == "columns": + elif orient == "columns" or orient == "tight": if columns is not None: - raise ValueError("cannot use columns parameter with orient='columns'") + raise ValueError(f"cannot use columns parameter with orient='{orient}'") else: # pragma: no cover raise ValueError("only recognize index or columns for orient") - return cls(data, index=index, columns=columns, dtype=dtype) + if orient != "tight": + return cls(data, index=index, columns=columns, dtype=dtype) + else: + realdata = data["data"] + + def create_index(indexlist, namelist): + if len(namelist) > 1: + index = MultiIndex.from_tuples(indexlist, names=namelist) + else: + index = Index(indexlist, name=namelist[0]) + return index + + index = create_index(data["index"], data["index_names"]) + columns = create_index(data["columns"], data["column_names"]) + return cls(realdata, index=index, columns=columns, dtype=dtype) def to_numpy( self, dtype=None, copy: bool = False, na_value=lib.no_default @@ -1388,6 +1423,9 @@ def to_dict(self, orient="dict", into=dict): - 'series' : dict like {column -> Series(values)} - 'split' : dict like {'index' -> [index], 'columns' -> [columns], 'data' -> [values]} + - 'tight' : dict like + {'index' -> [index], 'columns' -> [columns], 'data' -> [values], + 'index_names' -> [index.names], 'column_names' -> [column.names]} - 'records' : list like [{column -> value}, ... , {column -> value}] - 'index' : dict like {index -> {column -> value}} @@ -1395,6 +1433,9 @@ def to_dict(self, orient="dict", into=dict): Abbreviations are allowed. `s` indicates `series` and `sp` indicates `split`. + .. versionadded:: 1.1.0 + 'tight' as an allowed value for the ``orient`` argument + into : class, default dict The collections.abc.Mapping subclass used for all Mappings in the return value. Can be the actual class or an empty @@ -1444,6 +1485,10 @@ def to_dict(self, orient="dict", into=dict): >>> df.to_dict('index') {'row1': {'col1': 1, 'col2': 0.5}, 'row2': {'col1': 2, 'col2': 0.75}} + >>> df.to_dict('tight') + {'index': ['row1', 'row2'], 'columns': ['col1', 'col2'], + 'data': [[1, 0.5], [2, 0.75]], 'index_names': [None], 'column_names': [None]} + You can also specify the mapping type. >>> from collections import OrderedDict, defaultdict @@ -1519,6 +1564,23 @@ def to_dict(self, orient="dict", into=dict): ) ) + elif orient == "tight": + return into_c( + ( + ("index", self.index.tolist()), + ("columns", self.columns.tolist()), + ( + "data", + [ + list(map(com.maybe_box_datetimelike, t)) + for t in self.itertuples(index=False, name=None) + ], + ), + ("index_names", list(self.index.names)), + ("column_names", list(self.columns.names)), + ) + ) + elif orient == "series": return into_c((k, com.maybe_box_datetimelike(v)) for k, v in self.items()) diff --git a/pandas/tests/frame/methods/test_to_dict.py b/pandas/tests/frame/methods/test_to_dict.py index f1656b46cf356..f33dacd15a19f 100644 --- a/pandas/tests/frame/methods/test_to_dict.py +++ b/pandas/tests/frame/methods/test_to_dict.py @@ -5,7 +5,7 @@ import pytest import pytz -from pandas import DataFrame, Series, Timestamp +from pandas import DataFrame, Index, MultiIndex, Series, Timestamp import pandas._testing as tm @@ -271,3 +271,29 @@ def test_to_dict_orient_dtype(self): "c": type(df_dict["c"]), } assert result == expected + + @pytest.mark.parametrize( + "index", + [ + None, + Index(["aa", "bb"]), + Index(["aa", "bb"], name="cc"), + MultiIndex.from_tuples([("a", "b"), ("a", "c")]), + MultiIndex.from_tuples([("a", "b"), ("a", "c")], names=["n1", "n2"]), + ], + ) + @pytest.mark.parametrize( + "columns", + [ + ["x", "y"], + Index(["x", "y"]), + Index(["x", "y"], name="z"), + MultiIndex.from_tuples([("x", 1), ("y", 2)]), + MultiIndex.from_tuples([("x", 1), ("y", 2)], names=["z1", "z2"]), + ], + ) + def test_to_dict_orient_tight(self, index, columns): + df = DataFrame.from_records([[1, 3], [2, 4]], columns=columns, index=index,) + roundtrip = DataFrame.from_dict(df.to_dict(orient="tight"), orient="tight") + + tm.assert_frame_equal(df, roundtrip) From 520f9c524bc26f178b012af04e48a64aaad5f162 Mon Sep 17 00:00:00 2001 From: Irv Lustig Date: Wed, 15 Jul 2020 16:12:03 -0400 Subject: [PATCH 2/8] fix up documentation formatting --- doc/source/whatsnew/v1.1.0.rst | 11 +++++++---- pandas/core/frame.py | 16 ++++++++-------- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index e9d9d75e9a62f..93ee30b8be33f 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -277,14 +277,17 @@ DataFrame.from_dict and DataFrame.to_dict have new ``'tight'`` option ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ A new ``'tight'`` dictionary format that preserves :class:`MultiIndex` entries and names -is now available, and can be used with the standard `json` library to produce a tight +is now available with the :meth:`DataFrame.from_dict` and :meth:`DataFrame.to_dict` methods +and can be used with the standard ``json`` library to produce a tight representation of :class:`DataFrame` objects (:issue:`4889`). .. ipython:: python - df = pd.DataFrame.from_records([[1, 3], [2, 4]], - index=pd.MultiIndex.from_tuples([("a", "b"), ("a", "c")], names=["n1", "n2"]), - columns=pd.MultiIndex.from_tuples([("x", 1), ("y", 2)], names=["z1", "z2"])) + df = pd.DataFrame.from_records( + [[1, 3], [2, 4]], + index=pd.MultiIndex.from_tuples([("a", "b"), ("a", "c")], names=["n1", "n2"]), + columns=pd.MultiIndex.from_tuples([("x", 1), ("y", 2)], names=["z1", "z2"]), + ) df df.to_dict(orient='tight') diff --git a/pandas/core/frame.py b/pandas/core/frame.py index a6922062cac55..06746d9c0b66d 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -1243,8 +1243,8 @@ def from_dict(cls, data, orient="columns", dtype=None, columns=None) -> "DataFra If 'tight', assume a dict with keys ['index', 'columns', 'data', 'index_names', 'column_names'] - .. versionadded:: 1.1.0 - 'tight' as an allowed value for the ``orient`` argument + .. versionadded:: 1.1.0 + 'tight' as an allowed value for the ``orient`` argument dtype : dtype, default None Data type to force, otherwise infer. @@ -1289,10 +1289,10 @@ def from_dict(cls, data, orient="columns", dtype=None, columns=None) -> "DataFra Specify ``orient='tight'`` to create the DataFrame using a 'tight' format. >>> data = {'index': [('a', 'b'), ('a', 'c')], - 'columns': [('x', 1), ('y', 2)], - 'data': [[1, 3], [2, 4]], - 'index_names': ['n1', 'n2'], - 'column_names': ['z1', 'z2']} + ... 'columns': [('x', 1), ('y', 2)], + ... 'data': [[1, 3], [2, 4]], + ... 'index_names': ['n1', 'n2'], + ... 'column_names': ['z1', 'z2']} >>> pd.DataFrame.from_dict(data, orient='tight') z1 x y z2 1 2 @@ -1433,8 +1433,8 @@ def to_dict(self, orient="dict", into=dict): Abbreviations are allowed. `s` indicates `series` and `sp` indicates `split`. - .. versionadded:: 1.1.0 - 'tight' as an allowed value for the ``orient`` argument + .. versionadded:: 1.1.0 + 'tight' as an allowed value for the ``orient`` argument into : class, default dict The collections.abc.Mapping subclass used for all Mappings From e46d5acf2dbd816e5944b6c292f31cf23c606d1f Mon Sep 17 00:00:00 2001 From: Irv Lustig Date: Wed, 15 Jul 2020 16:33:55 -0400 Subject: [PATCH 3/8] code formatting in docs --- doc/source/whatsnew/v1.1.0.rst | 6 ++++-- pandas/core/frame.py | 18 +++++++++--------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index 93ee30b8be33f..fae33bd184c9f 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -285,8 +285,10 @@ representation of :class:`DataFrame` objects (:issue:`4889`). df = pd.DataFrame.from_records( [[1, 3], [2, 4]], - index=pd.MultiIndex.from_tuples([("a", "b"), ("a", "c")], names=["n1", "n2"]), - columns=pd.MultiIndex.from_tuples([("x", 1), ("y", 2)], names=["z1", "z2"]), + index=pd.MultiIndex.from_tuples([("a", "b"), ("a", "c")], + names=["n1", "n2"]), + columns=pd.MultiIndex.from_tuples([("x", 1), ("y", 2)], + names=["z1", "z2"]), ) df df.to_dict(orient='tight') diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 06746d9c0b66d..df32c8497c1d8 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -1286,6 +1286,15 @@ def from_dict(cls, data, orient="columns", dtype=None, columns=None) -> "DataFra row_1 3 2 1 0 row_2 a b c d + When using the 'index' orientation, the column names can be + specified manually: + + >>> pd.DataFrame.from_dict(data, orient='index', + ... columns=['A', 'B', 'C', 'D']) + A B C D + row_1 3 2 1 0 + row_2 a b c d + Specify ``orient='tight'`` to create the DataFrame using a 'tight' format. >>> data = {'index': [('a', 'b'), ('a', 'c')], @@ -1299,15 +1308,6 @@ def from_dict(cls, data, orient="columns", dtype=None, columns=None) -> "DataFra n1 n2 a b 1 3 c 2 4 - - When using the 'index' orientation, the column names can be - specified manually: - - >>> pd.DataFrame.from_dict(data, orient='index', - ... columns=['A', 'B', 'C', 'D']) - A B C D - row_1 3 2 1 0 - row_2 a b c d """ index = None orient = orient.lower() From 06c3f2aec124f78a5786b14650f9b5a4cce2131d Mon Sep 17 00:00:00 2001 From: Irv Lustig Date: Wed, 15 Jul 2020 17:12:38 -0400 Subject: [PATCH 4/8] fix indentation in docstrings --- doc/source/whatsnew/v1.1.0.rst | 2 +- pandas/core/frame.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index fae33bd184c9f..5766681845bd9 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -288,7 +288,7 @@ representation of :class:`DataFrame` objects (:issue:`4889`). index=pd.MultiIndex.from_tuples([("a", "b"), ("a", "c")], names=["n1", "n2"]), columns=pd.MultiIndex.from_tuples([("x", 1), ("y", 2)], - names=["z1", "z2"]), + names=["z1", "z2"]), ) df df.to_dict(orient='tight') diff --git a/pandas/core/frame.py b/pandas/core/frame.py index df32c8497c1d8..61afe3848dc7d 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -1296,7 +1296,7 @@ def from_dict(cls, data, orient="columns", dtype=None, columns=None) -> "DataFra row_2 a b c d Specify ``orient='tight'`` to create the DataFrame using a 'tight' - format. + format: >>> data = {'index': [('a', 'b'), ('a', 'c')], ... 'columns': [('x', 1), ('y', 2)], ... 'data': [[1, 3], [2, 4]], @@ -1425,7 +1425,7 @@ def to_dict(self, orient="dict", into=dict): {'index' -> [index], 'columns' -> [columns], 'data' -> [values]} - 'tight' : dict like {'index' -> [index], 'columns' -> [columns], 'data' -> [values], - 'index_names' -> [index.names], 'column_names' -> [column.names]} + 'index_names' -> [index.names], 'column_names' -> [column.names]} - 'records' : list like [{column -> value}, ... , {column -> value}] - 'index' : dict like {index -> {column -> value}} From d8e68f9ccd9d8495e35c26cc9e6ce216187dd322 Mon Sep 17 00:00:00 2001 From: Irv Lustig Date: Wed, 15 Jul 2020 17:43:43 -0400 Subject: [PATCH 5/8] add period to docstring for from_dict orient --- pandas/core/frame.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 61afe3848dc7d..cccc20985c871 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -1241,7 +1241,7 @@ def from_dict(cls, data, orient="columns", dtype=None, columns=None) -> "DataFra should be the columns of the resulting DataFrame, pass 'columns' (default). Otherwise if the keys should be rows, pass 'index'. If 'tight', assume a dict with keys ['index', 'columns', 'data', - 'index_names', 'column_names'] + 'index_names', 'column_names']. .. versionadded:: 1.1.0 'tight' as an allowed value for the ``orient`` argument From 1ef32fcffa39861eee341b6b6b38e9c9935b9be4 Mon Sep 17 00:00:00 2001 From: Irv Lustig Date: Wed, 15 Jul 2020 20:09:48 -0400 Subject: [PATCH 6/8] add blank line in docstring --- pandas/core/frame.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index cccc20985c871..9612b9c7dd371 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -1297,6 +1297,7 @@ def from_dict(cls, data, orient="columns", dtype=None, columns=None) -> "DataFra Specify ``orient='tight'`` to create the DataFrame using a 'tight' format: + >>> data = {'index': [('a', 'b'), ('a', 'c')], ... 'columns': [('x', 1), ('y', 2)], ... 'data': [[1, 3], [2, 4]], From f7998d3ab62d7c8329eb562807eae55eb7185852 Mon Sep 17 00:00:00 2001 From: Irv Lustig Date: Wed, 6 Jan 2021 11:12:52 -0500 Subject: [PATCH 7/8] WIP on adding to_dict --- doc/source/whatsnew/v1.1.0.rst | 22 ---------------------- doc/source/whatsnew/v1.2.0.rst | 22 ++++++++++++++++++++++ pandas/core/frame.py | 4 ++-- test_fast.bat | 2 +- 4 files changed, 25 insertions(+), 25 deletions(-) diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index 6f5ca612701d0..a49b29d691692 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -269,28 +269,6 @@ change, as ``fsspec`` will still bring in the same packages as before. .. _fsspec docs: https://filesystem-spec.readthedocs.io/en/latest/ -.. _whatsnew_110.dict_tight: - -DataFrame.from_dict and DataFrame.to_dict have new ``'tight'`` option -^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - -A new ``'tight'`` dictionary format that preserves :class:`MultiIndex` entries and names -is now available with the :meth:`DataFrame.from_dict` and :meth:`DataFrame.to_dict` methods -and can be used with the standard ``json`` library to produce a tight -representation of :class:`DataFrame` objects (:issue:`4889`). - -.. ipython:: python - - df = pd.DataFrame.from_records( - [[1, 3], [2, 4]], - index=pd.MultiIndex.from_tuples([("a", "b"), ("a", "c")], - names=["n1", "n2"]), - columns=pd.MultiIndex.from_tuples([("x", 1), ("y", 2)], - names=["z1", "z2"]), - ) - df - df.to_dict(orient='tight') - .. _whatsnew_110.enhancements.other: Other enhancements diff --git a/doc/source/whatsnew/v1.2.0.rst b/doc/source/whatsnew/v1.2.0.rst index 2a8b6fe3ade6a..3ae3761ef0e33 100644 --- a/doc/source/whatsnew/v1.2.0.rst +++ b/doc/source/whatsnew/v1.2.0.rst @@ -109,6 +109,28 @@ Beginning with this version, the default is now to use the more accurate parser ``floating_precision="legacy"`` to use the legacy parser. The change to using the higher precision parser by default should have no impact on performance. (:issue:`17154`) +.. _whatsnew_120.dict_tight: + +DataFrame.from_dict and DataFrame.to_dict have new ``'tight'`` option +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +A new ``'tight'`` dictionary format that preserves :class:`MultiIndex` entries and names +is now available with the :meth:`DataFrame.from_dict` and :meth:`DataFrame.to_dict` methods +and can be used with the standard ``json`` library to produce a tight +representation of :class:`DataFrame` objects (:issue:`4889`). + +.. ipython:: python + + df = pd.DataFrame.from_records( + [[1, 3], [2, 4]], + index=pd.MultiIndex.from_tuples([("a", "b"), ("a", "c")], + names=["n1", "n2"]), + columns=pd.MultiIndex.from_tuples([("x", 1), ("y", 2)], + names=["z1", "z2"]), + ) + df + df.to_dict(orient='tight') + .. _whatsnew_120.enhancements.other: Other enhancements diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 9cd81c507f6f1..09f37d6190ab6 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -1240,7 +1240,7 @@ def from_dict(cls, data, orient="columns", dtype=None, columns=None) -> DataFram If 'tight', assume a dict with keys ['index', 'columns', 'data', 'index_names', 'column_names']. - .. versionadded:: 1.1.0 + .. versionadded:: 1.2.0 'tight' as an allowed value for the ``orient`` argument dtype : dtype, default None @@ -1431,7 +1431,7 @@ def to_dict(self, orient="dict", into=dict): Abbreviations are allowed. `s` indicates `series` and `sp` indicates `split`. - .. versionadded:: 1.1.0 + .. versionadded:: 1.2.0 'tight' as an allowed value for the ``orient`` argument into : class, default dict diff --git a/test_fast.bat b/test_fast.bat index f2c4e9fa71fcd..6eea5fed53e55 100644 --- a/test_fast.bat +++ b/test_fast.bat @@ -1,3 +1,3 @@ :: test on windows set PYTHONHASHSEED=314159265 -pytest --skip-slow --skip-network --skip-db -m "not single" -n 4 -r sXX --strict pandas +pytest --skip-slow --skip-network --skip-db -m "not single" -n 8 -r sXX --strict pandas From 77fec12c8d78d64a7822c5223e12ffa9174c94e7 Mon Sep 17 00:00:00 2001 From: Irv Lustig Date: Mon, 4 Oct 2021 09:50:18 -0400 Subject: [PATCH 8/8] change versionchanged to 1.4.0. Fix typing issue, update API calls --- pandas/core/frame.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 815c250790e59..093ccdf1de6f9 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -1543,7 +1543,7 @@ def from_dict( If 'tight', assume a dict with keys ['index', 'columns', 'data', 'index_names', 'column_names']. - .. versionadded:: 1.2.0 + .. versionadded:: 1.4.0 'tight' as an allowed value for the ``orient`` argument dtype : dtype, default None @@ -1629,6 +1629,7 @@ def from_dict( realdata = data["data"] def create_index(indexlist, namelist): + index: Index if len(namelist) > 1: index = MultiIndex.from_tuples(indexlist, names=namelist) else: @@ -1737,7 +1738,7 @@ def to_dict(self, orient: str = "dict", into=dict): Abbreviations are allowed. `s` indicates `series` and `sp` indicates `split`. - .. versionadded:: 1.2.0 + .. versionadded:: 1.4.0 'tight' as an allowed value for the ``orient`` argument into : class, default dict @@ -1877,7 +1878,7 @@ def to_dict(self, orient: str = "dict", into=dict): ( "data", [ - list(map(com.maybe_box_datetimelike, t)) + list(map(maybe_box_native, t)) for t in self.itertuples(index=False, name=None) ], ),