@@ -1140,6 +1140,13 @@ def _handle_meta(meta, title):
1140
1140
f"instead of { type (meta ).__name__ } " )
1141
1141
return Metadata (meta )
1142
1142
1143
+ # This prevents a warning in Pandas 1.4 <= version < 2.0 for arrays with object
1144
+ # dtype which contain only numeric values. We force Pandas 2.0 behavior
1145
+ # (ie use object dtype instead of inferring). See issue #1061.
1146
+ def np_array_to_pd_index (array , name = None , tupleize_cols = True ):
1147
+ dtype = None if array .dtype .kind != 'O' else object
1148
+ return pd .Index (array , dtype = dtype , name = name , tupleize_cols = tupleize_cols )
1149
+
1143
1150
1144
1151
class Array (ABCArray ):
1145
1152
r"""
@@ -1538,17 +1545,17 @@ def to_frame(self, fold_last_axis_name=False, dropna=None) -> pd.DataFrame:
1538
1545
a1 b0 4 5
1539
1546
b1 6 7
1540
1547
"""
1541
- columns = pd . Index ( self .axes [- 1 ].labels )
1542
- if not fold_last_axis_name :
1543
- columns . name = self .axes [- 1 ].name
1548
+ last_name = self .axes [- 1 ].name
1549
+ columns_name = None if fold_last_axis_name else last_name
1550
+ columns = np_array_to_pd_index ( self .axes [- 1 ].labels , name = columns_name )
1544
1551
if self .ndim > 1 :
1545
1552
axes_names = self .axes .names [:- 1 ]
1546
1553
if fold_last_axis_name :
1547
1554
tmp = axes_names [- 1 ] if axes_names [- 1 ] is not None else ''
1548
- if self . axes [ - 1 ]. name :
1549
- axes_names [- 1 ] = f"{ tmp } \\ { self . axes [ - 1 ]. name } "
1555
+ if last_name :
1556
+ axes_names [- 1 ] = f"{ tmp } \\ { last_name } "
1550
1557
if self .ndim == 2 :
1551
- index = pd . Index ( data = self .axes [0 ].labels , name = axes_names [0 ])
1558
+ index = np_array_to_pd_index ( self .axes [0 ].labels , name = axes_names [0 ])
1552
1559
else :
1553
1560
index = pd .MultiIndex .from_product (self .axes .labels [:- 1 ], names = axes_names )
1554
1561
else :
@@ -1632,7 +1639,9 @@ def to_series(self, name=None, dropna=False) -> pd.Series:
1632
1639
elif self .ndim == 1 :
1633
1640
axis = self .axes [0 ]
1634
1641
# Note that string labels will be converted to object dtype in the process
1635
- index = pd .Index (axis .labels , name = axis .name , tupleize_cols = False )
1642
+ # and label arrays with object dtype containing only numeric values will keep
1643
+ # the object dtype.
1644
+ index = np_array_to_pd_index (axis .labels , name = axis .name , tupleize_cols = False )
1636
1645
else :
1637
1646
index = pd .MultiIndex .from_product (self .axes .labels , names = self .axes .names )
1638
1647
series = pd .Series (self .data .reshape (- 1 ), index , name = name )
0 commit comments