Skip to content

ENH - Add index parameter do df.to_dict() #47657

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.5.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ Other enhancements
- :meth:`DatetimeIndex.astype` now supports casting timezone-naive indexes to ``datetime64[s]``, ``datetime64[ms]``, and ``datetime64[us]``, and timezone-aware indexes to the corresponding ``datetime64[unit, tzname]`` dtypes (:issue:`47579`)
- :class:`Series` reducers (e.g. ``min``, ``max``, ``sum``, ``mean``) will now successfully operate when the dtype is numeric and ``numeric_only=True`` is provided; previously this would raise a ``NotImplementedError`` (:issue:`47500`)
- :meth:`RangeIndex.union` now can return a :class:`RangeIndex` instead of a :class:`Int64Index` if the resulting values are equally spaced (:issue:`47557`, :issue:`43885`)
- :meth:`DataFrame.to_dict` now has a ``index`` parameter with default value True that can be set to False to exclude the index from the dictionary if orient is split or tight (:issue:`46398`)

.. ---------------------------------------------------------------------------
.. _whatsnew_150.notable_bug_fixes:
Expand Down
86 changes: 61 additions & 25 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1811,7 +1811,7 @@ def to_numpy(

return result

def to_dict(self, orient: str = "dict", into=dict):
def to_dict(self, orient: str = "dict", into=dict, index=True):
"""
Convert the DataFrame to a dictionary.

Expand Down Expand Up @@ -1847,6 +1847,12 @@ def to_dict(self, orient: str = "dict", into=dict):
instance of the mapping type you want. If you want a
collections.defaultdict, you must pass it initialized.

index : bool, default True
When set to False, method returns a dict without an index key
(and index_names if using orient='tight').

.. versionadded:: 1.5.0

Returns
-------
dict, list or collections.abc.Mapping
Expand Down Expand Up @@ -1958,36 +1964,66 @@ def to_dict(self, orient: str = "dict", into=dict):
)

elif orient == "split":
return into_c(
(
("index", self.index.tolist()),
("columns", self.columns.tolist()),

if not index:
return into_c(
(
"data",
[
list(map(maybe_box_native, t))
for t in self.itertuples(index=False, name=None)
],
),
("columns", self.columns.tolist()),
(
"data",
[
list(map(maybe_box_native, t))
for t in self.itertuples(index=False, name=None)
],
),
)
)
else:
return into_c(
(
("index", self.index.tolist()),
("columns", self.columns.tolist()),
(
"data",
[
list(map(maybe_box_native, t))
for t in self.itertuples(index=False, name=None)
],
),
)
)
)

elif orient == "tight":
return into_c(
(
("index", self.index.tolist()),
("columns", self.columns.tolist()),
if not index:
return into_c(
(
("columns", self.columns.tolist()),
(
"data",
[
list(map(maybe_box_native, t))
for t in self.itertuples(index=False, name=None)
],
),
("column_names", list(self.columns.names)),
)
)
else:
return into_c(
(
"data",
[
list(map(maybe_box_native, t))
for t in self.itertuples(index=False, name=None)
],
),
("index_names", list(self.index.names)),
("column_names", list(self.columns.names)),
("index", self.index.tolist()),
("columns", self.columns.tolist()),
(
"data",
[
list(map(maybe_box_native, 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, v) for k, v in self.items())
Expand Down
21 changes: 21 additions & 0 deletions pandas/tests/frame/methods/test_to_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,3 +421,24 @@ def test_to_dict_returns_native_types(self, orient, data, expected_types):
for i, key, value in assertion_iterator:
assert value == data[key][i]
assert type(value) is expected_types[key][i]

def test_to_dict_index_orient_split(self):
# GH#46398
df = DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]}, index=["row1", "row2", "row3"])
result = df.to_dict(orient="split", index=False)
expected = {
"columns": ["A", "B"],
"data": [[1, 4], [2, 5], [3, 6]],
}
tm.assert_dict_equal(result, expected)

def test_to_dict_index_orient_tight(self):
# GH#46398
df = DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]}, index=["row1", "row2", "row3"])
result = df.to_dict(orient="tight", index=False)
expected = {
"columns": ["A", "B"],
"data": [[1, 4], [2, 5], [3, 6]],
"column_names": [None],
}
tm.assert_dict_equal(result, expected)