Skip to content

Commit d9e67c9

Browse files
committed
always reset_index when inferring index dtypes
1 parent 40762ab commit d9e67c9

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

awswrangler/_data_types.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -467,13 +467,12 @@ def pyarrow_types_from_pandas( # pylint: disable=too-many-branches
467467
# Filling indexes
468468
indexes: List[str] = []
469469
if index is True:
470-
try:
471-
fields = pa.Schema.from_pandas(df=df[[]], preserve_index=True)
472-
except AttributeError as ae:
473-
if "'Index' object has no attribute 'head'" not in str(ae):
474-
raise ae
475-
# Get index fields from a new df with only index columns
476-
fields = pa.Schema.from_pandas(df=df.reset_index().drop(columns=cols), preserve_index=False)
470+
# Get index columns
471+
# Adding indexes as columns via .reset_index() because
472+
# pa.Schema.from_pandas(.., preserve_index=True) fails with
473+
# "'Index' object has no attribute 'head'" if using extension
474+
# dtypes on pandas 1.4.x
475+
fields = pa.Schema.from_pandas(df=df[[]].reset_index(), preserve_index=False)
477476
for field in fields:
478477
name = str(field.name)
479478
_logger.debug("Inferring PyArrow type from index: %s", name)

0 commit comments

Comments
 (0)