@@ -1932,16 +1932,15 @@ def to_dict(self, orient: str = "dict", into=dict):
1932
1932
for t in self .itertuples (index = False , name = None )
1933
1933
]
1934
1934
elif object_dtype_cols :
1935
- is_object_dtype_by_index = [
1936
- col in object_dtype_cols for col in self .columns
1937
- ]
1938
- data = [
1939
- [
1940
- maybe_box_native (v ) if is_object_dtype_by_index [i ] else v
1941
- for i , v in enumerate (t )
1942
- ]
1943
- for t in self .itertuples (index = False , name = None )
1935
+ # A number of ways were tried here, this solution proved to be the
1936
+ # most optimal in general
1937
+ data = [list (t ) for t in self .itertuples (index = False , name = None )]
1938
+ object_type_indices = [
1939
+ i for i , col in enumerate (self .columns ) if col in object_dtype_cols
1944
1940
]
1941
+ for row in data :
1942
+ for i in object_type_indices :
1943
+ row [i ] = maybe_box_native (row [i ])
1945
1944
else :
1946
1945
data = [list (t ) for t in self .itertuples (index = False , name = None )]
1947
1946
return into_c (
@@ -1955,7 +1954,16 @@ def to_dict(self, orient: str = "dict", into=dict):
1955
1954
return into_c ((k , v ) for k , v in self .items ())
1956
1955
elif orient == "records" :
1957
1956
columns = self .columns .tolist ()
1958
- if object_dtype_cols :
1957
+ if are_all_object_dtype_cols :
1958
+ rows = (
1959
+ dict (zip (columns , row ))
1960
+ for row in self .itertuples (index = False , name = None )
1961
+ )
1962
+ return [
1963
+ into_c ((k , maybe_box_native (v )) for k , v in row .items ())
1964
+ for row in rows
1965
+ ]
1966
+ elif object_dtype_cols :
1959
1967
is_object_dtype_by_index = [col in object_dtype_cols for col in columns ]
1960
1968
return [
1961
1969
into_c (
@@ -1980,7 +1988,12 @@ def to_dict(self, orient: str = "dict", into=dict):
1980
1988
if not self .index .is_unique :
1981
1989
raise ValueError ("DataFrame index must be unique for orient='index'." )
1982
1990
columns = self .columns .tolist ()
1983
- if object_dtype_cols :
1991
+ if are_all_object_dtype_cols :
1992
+ return into_c (
1993
+ (t [0 ], dict (zip (self .columns , map (maybe_box_native , t [1 :]))))
1994
+ for t in self .itertuples (name = None )
1995
+ )
1996
+ elif object_dtype_cols :
1984
1997
is_object_dtype_by_index = [
1985
1998
col in object_dtype_cols for col in self .columns
1986
1999
]
@@ -1998,10 +2011,7 @@ def to_dict(self, orient: str = "dict", into=dict):
1998
2011
)
1999
2012
else :
2000
2013
return into_c (
2001
- (
2002
- t [0 ],
2003
- {columns [i ]: v for i , v in enumerate (t [1 :])},
2004
- )
2014
+ (t [0 ], dict (zip (self .columns , t [1 :])))
2005
2015
for t in self .itertuples (name = None )
2006
2016
)
2007
2017
elif orient == "tight" :
@@ -2011,16 +2021,15 @@ def to_dict(self, orient: str = "dict", into=dict):
2011
2021
for t in self .itertuples (index = False , name = None )
2012
2022
]
2013
2023
elif object_dtype_cols :
2014
- is_object_dtype_by_index = [
2015
- col in object_dtype_cols for col in self .columns
2016
- ]
2017
- data = [
2018
- [
2019
- maybe_box_native (v ) if is_object_dtype_by_index [i ] else v
2020
- for i , v in enumerate (t )
2021
- ]
2022
- for t in self .itertuples (index = False , name = None )
2024
+ # A number of ways were tried here, this solution proved to be the
2025
+ # most optimal in general
2026
+ data = [list (t ) for t in self .itertuples (index = False , name = None )]
2027
+ object_type_indices = [
2028
+ i for i , col in enumerate (self .columns ) if col in object_dtype_cols
2023
2029
]
2030
+ for row in data :
2031
+ for i in object_type_indices :
2032
+ row [i ] = maybe_box_native (row [i ])
2024
2033
else :
2025
2034
data = [list (t ) for t in self .itertuples (index = False , name = None )]
2026
2035
return into_c (
0 commit comments