to_dict.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. from __future__ import annotations
  2. from typing import Literal
  3. import warnings
  4. from pandas.util._exceptions import find_stack_level
  5. from pandas.core.dtypes.cast import maybe_box_native
  6. from pandas.core.dtypes.common import (
  7. is_extension_array_dtype,
  8. is_object_dtype,
  9. )
  10. from pandas import DataFrame
  11. from pandas.core import common as com
  12. def to_dict(
  13. df: DataFrame,
  14. orient: Literal[
  15. "dict", "list", "series", "split", "tight", "records", "index"
  16. ] = "dict",
  17. into: type[dict] = dict,
  18. index: bool = True,
  19. ) -> dict | list[dict]:
  20. """
  21. Convert the DataFrame to a dictionary.
  22. The type of the key-value pairs can be customized with the parameters
  23. (see below).
  24. Parameters
  25. ----------
  26. orient : str {'dict', 'list', 'series', 'split', 'tight', 'records', 'index'}
  27. Determines the type of the values of the dictionary.
  28. - 'dict' (default) : dict like {column -> {index -> value}}
  29. - 'list' : dict like {column -> [values]}
  30. - 'series' : dict like {column -> Series(values)}
  31. - 'split' : dict like
  32. {'index' -> [index], 'columns' -> [columns], 'data' -> [values]}
  33. - 'tight' : dict like
  34. {'index' -> [index], 'columns' -> [columns], 'data' -> [values],
  35. 'index_names' -> [index.names], 'column_names' -> [column.names]}
  36. - 'records' : list like
  37. [{column -> value}, ... , {column -> value}]
  38. - 'index' : dict like {index -> {column -> value}}
  39. .. versionadded:: 1.4.0
  40. 'tight' as an allowed value for the ``orient`` argument
  41. into : class, default dict
  42. The collections.abc.Mapping subclass used for all Mappings
  43. in the return value. Can be the actual class or an empty
  44. instance of the mapping type you want. If you want a
  45. collections.defaultdict, you must pass it initialized.
  46. index : bool, default True
  47. Whether to include the index item (and index_names item if `orient`
  48. is 'tight') in the returned dictionary. Can only be ``False``
  49. when `orient` is 'split' or 'tight'.
  50. .. versionadded:: 2.0.0
  51. Returns
  52. -------
  53. dict, list or collections.abc.Mapping
  54. Return a collections.abc.Mapping object representing the DataFrame.
  55. The resulting transformation depends on the `orient` parameter.
  56. """
  57. if not df.columns.is_unique:
  58. warnings.warn(
  59. "DataFrame columns are not unique, some columns will be omitted.",
  60. UserWarning,
  61. stacklevel=find_stack_level(),
  62. )
  63. # GH16122
  64. into_c = com.standardize_mapping(into)
  65. # error: Incompatible types in assignment (expression has type "str",
  66. # variable has type "Literal['dict', 'list', 'series', 'split', 'tight',
  67. # 'records', 'index']")
  68. orient = orient.lower() # type: ignore[assignment]
  69. if not index and orient not in ["split", "tight"]:
  70. raise ValueError(
  71. "'index=False' is only valid when 'orient' is 'split' or 'tight'"
  72. )
  73. if orient == "series":
  74. # GH46470 Return quickly if orient series to avoid creating dtype objects
  75. return into_c((k, v) for k, v in df.items())
  76. box_native_indices = [
  77. i
  78. for i, col_dtype in enumerate(df.dtypes.values)
  79. if is_object_dtype(col_dtype) or is_extension_array_dtype(col_dtype)
  80. ]
  81. are_all_object_dtype_cols = len(box_native_indices) == len(df.dtypes)
  82. if orient == "dict":
  83. return into_c((k, v.to_dict(into)) for k, v in df.items())
  84. elif orient == "list":
  85. object_dtype_indices_as_set = set(box_native_indices)
  86. return into_c(
  87. (
  88. k,
  89. list(map(maybe_box_native, v.tolist()))
  90. if i in object_dtype_indices_as_set
  91. else v.tolist(),
  92. )
  93. for i, (k, v) in enumerate(df.items())
  94. )
  95. elif orient == "split":
  96. data = df._create_data_for_split_and_tight_to_dict(
  97. are_all_object_dtype_cols, box_native_indices
  98. )
  99. return into_c(
  100. ((("index", df.index.tolist()),) if index else ())
  101. + (
  102. ("columns", df.columns.tolist()),
  103. ("data", data),
  104. )
  105. )
  106. elif orient == "tight":
  107. data = df._create_data_for_split_and_tight_to_dict(
  108. are_all_object_dtype_cols, box_native_indices
  109. )
  110. return into_c(
  111. ((("index", df.index.tolist()),) if index else ())
  112. + (
  113. ("columns", df.columns.tolist()),
  114. (
  115. "data",
  116. [
  117. list(map(maybe_box_native, t))
  118. for t in df.itertuples(index=False, name=None)
  119. ],
  120. ),
  121. )
  122. + ((("index_names", list(df.index.names)),) if index else ())
  123. + (("column_names", list(df.columns.names)),)
  124. )
  125. elif orient == "records":
  126. columns = df.columns.tolist()
  127. if are_all_object_dtype_cols:
  128. rows = (
  129. dict(zip(columns, row)) for row in df.itertuples(index=False, name=None)
  130. )
  131. return [
  132. into_c((k, maybe_box_native(v)) for k, v in row.items()) for row in rows
  133. ]
  134. else:
  135. data = [
  136. into_c(zip(columns, t)) for t in df.itertuples(index=False, name=None)
  137. ]
  138. if box_native_indices:
  139. object_dtype_indices_as_set = set(box_native_indices)
  140. object_dtype_cols = {
  141. col
  142. for i, col in enumerate(df.columns)
  143. if i in object_dtype_indices_as_set
  144. }
  145. for row in data:
  146. for col in object_dtype_cols:
  147. row[col] = maybe_box_native(row[col])
  148. return data
  149. elif orient == "index":
  150. if not df.index.is_unique:
  151. raise ValueError("DataFrame index must be unique for orient='index'.")
  152. columns = df.columns.tolist()
  153. if are_all_object_dtype_cols:
  154. return into_c(
  155. (t[0], dict(zip(df.columns, map(maybe_box_native, t[1:]))))
  156. for t in df.itertuples(name=None)
  157. )
  158. elif box_native_indices:
  159. object_dtype_indices_as_set = set(box_native_indices)
  160. is_object_dtype_by_index = [
  161. i in object_dtype_indices_as_set for i in range(len(df.columns))
  162. ]
  163. return into_c(
  164. (
  165. t[0],
  166. {
  167. columns[i]: maybe_box_native(v)
  168. if is_object_dtype_by_index[i]
  169. else v
  170. for i, v in enumerate(t[1:])
  171. },
  172. )
  173. for t in df.itertuples(name=None)
  174. )
  175. else:
  176. return into_c(
  177. (t[0], dict(zip(df.columns, t[1:]))) for t in df.itertuples(name=None)
  178. )
  179. else:
  180. raise ValueError(f"orient '{orient}' not understood")