Skip to content

Commit 1fdd0d6

Browse files
Vruthik ThakkarVruthik Thakkar
Vruthik Thakkar
authored and
Vruthik Thakkar
committed
ENH: add index param to to_dict method pandas-dev#46398
1 parent dafa5dd commit 1fdd0d6

File tree

1 file changed

+64
-25
lines changed

1 file changed

+64
-25
lines changed

pandas/core/frame.py

+64-25
Original file line numberDiff line numberDiff line change
@@ -1771,7 +1771,7 @@ def to_numpy(
17711771

17721772
return result
17731773

1774-
def to_dict(self, orient: str = "dict", into=dict):
1774+
def to_dict(self, orient: str = "dict", into=dict, index=True):
17751775
"""
17761776
Convert the DataFrame to a dictionary.
17771777
@@ -1807,6 +1807,11 @@ def to_dict(self, orient: str = "dict", into=dict):
18071807
instance of the mapping type you want. If you want a
18081808
collections.defaultdict, you must pass it initialized.
18091809
1810+
index : bool default True
1811+
When set to False, method returns a dict without an index key
1812+
(and index_names if using orient='tight'). Can only be False
1813+
when orient='split' or 'tight'.
1814+
18101815
Returns
18111816
-------
18121817
dict, list or collections.abc.Mapping
@@ -1909,43 +1914,77 @@ def to_dict(self, orient: str = "dict", into=dict):
19091914
elif orient.startswith("i"):
19101915
orient = "index"
19111916

1917+
if not index and orient not in ['split', 'tight']:
1918+
raise ValueError(
1919+
"'index=False' is only valid when 'orient' is 'split' or 'tight"
1920+
)
1921+
19121922
if orient == "dict":
19131923
return into_c((k, v.to_dict(into)) for k, v in self.items())
19141924

19151925
elif orient == "list":
19161926
return into_c((k, v.tolist()) for k, v in self.items())
19171927

19181928
elif orient == "split":
1919-
return into_c(
1920-
(
1921-
("index", self.index.tolist()),
1922-
("columns", self.columns.tolist()),
1929+
if not index:
1930+
return into_c(
19231931
(
1924-
"data",
1925-
[
1926-
list(map(maybe_box_native, t))
1927-
for t in self.itertuples(index=False, name=None)
1928-
],
1929-
),
1932+
("columns", self.columns.tolist()),
1933+
(
1934+
"data",
1935+
[
1936+
list(map(maybe_box_native, t))
1937+
for t in self.itertuples(index=False, name=None)
1938+
],
1939+
),
1940+
)
1941+
)
1942+
else:
1943+
return into_c(
1944+
(
1945+
("index", self.index.tolist()),
1946+
("columns", self.columns.tolist()),
1947+
(
1948+
"data",
1949+
[
1950+
list(map(maybe_box_native, t))
1951+
for t in self.itertuples(index=False, name=None)
1952+
],
1953+
),
1954+
)
19301955
)
1931-
)
19321956

19331957
elif orient == "tight":
1934-
return into_c(
1935-
(
1936-
("index", self.index.tolist()),
1937-
("columns", self.columns.tolist()),
1958+
if not index:
1959+
return into_c(
1960+
(
1961+
("columns", self.columns.tolist()),
1962+
(
1963+
"data",
1964+
[
1965+
list(map(maybe_box_native, t))
1966+
for t in self.itertuples(index=False, name=None)
1967+
],
1968+
),
1969+
("column_names", list(self.columns.names)),
1970+
)
1971+
)
1972+
else:
1973+
return into_c(
19381974
(
1939-
"data",
1940-
[
1941-
list(map(maybe_box_native, t))
1942-
for t in self.itertuples(index=False, name=None)
1943-
],
1944-
),
1945-
("index_names", list(self.index.names)),
1946-
("column_names", list(self.columns.names)),
1975+
("index", self.index.tolist()),
1976+
("columns", self.columns.tolist()),
1977+
(
1978+
"data",
1979+
[
1980+
list(map(maybe_box_native, t))
1981+
for t in self.itertuples(index=False, name=None)
1982+
],
1983+
),
1984+
("index_names", list(self.index.names)),
1985+
("column_names", list(self.columns.names)),
1986+
)
19471987
)
1948-
)
19491988

19501989
elif orient == "series":
19511990
return into_c((k, v) for k, v in self.items())

0 commit comments

Comments
 (0)