Skip to content

Commit 917fafb

Browse files
committed
FIX: displaying Pandas 3+ DataFrame and Series when pyarrow not installed (fixed larray-project#322)
also cleanups the code in the process
1 parent 1187721 commit 917fafb

2 files changed

Lines changed: 13 additions & 20 deletions

File tree

‎doc/source/changes/version_0_35_2.rst.inc‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,7 @@ Fixes
4343

4444
* remove no longer displayable variables from the variable list (for example,
4545
when a variable represents an open file and the file is closed).
46-
Closes :editor_issue:`327`).
46+
Closes :editor_issue:`327`).
47+
48+
* fixed displaying Pandas 3+ DataFrame and Series when pyarrow is not installed
49+
(closes :editor_issue:`322`).

‎larray_editor/arrayadapter.py‎

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1882,36 +1882,26 @@ def get_hnames(self):
18821882
def get_vnames(self):
18831883
return self.data.index.names
18841884

1885-
@staticmethod
1886-
def _ensure_numpy_array(data):
1887-
"""Convert data to a numpy array if it is not already one."""
1888-
pd = sys.modules['pandas']
1889-
if isinstance(data, pd.arrays.ArrowStringArray):
1890-
return data.to_numpy()
1891-
else:
1892-
assert isinstance(data, np.ndarray)
1893-
return data
1894-
18951885
def get_vlabels_values(self, start, stop):
18961886
pd = sys.modules['pandas']
18971887
index = self.sorted_data.index[start:stop]
18981888
if isinstance(index, pd.MultiIndex):
18991889
# It seems like Pandas always returns a 1D array of tuples for
1900-
# MultiIndex.values, even if the MultiIndex has an homoneneous
1901-
# string type. That's why we do not need _ensure_numpy_array here
1890+
# MultiIndex.to_numpy(), even if the MultiIndex has an homogeneous
1891+
# string type.
19021892
# list(row) because we want a list of list and not a list of tuples
1903-
return [list(row) for row in index.values]
1893+
return [list(row) for row in index.to_numpy()]
19041894
else:
1905-
return self._ensure_numpy_array(index.values)[:, np.newaxis]
1895+
return index.to_numpy()[:, np.newaxis]
19061896

19071897
def get_hlabels_values(self, start, stop):
19081898
pd = sys.modules['pandas']
19091899
index = self.sorted_data.columns[start:stop]
19101900
if isinstance(index, pd.MultiIndex):
1911-
return [self._ensure_numpy_array(index.get_level_values(i).values)
1901+
return [index.get_level_values(i).to_numpy()
19121902
for i in range(index.nlevels)]
19131903
else:
1914-
return [self._ensure_numpy_array(index.values)]
1904+
return [index.to_numpy()]
19151905

19161906
def get_values(self, h_start, v_start, h_stop, v_stop):
19171907
pd = sys.modules['pandas']
@@ -2015,16 +2005,16 @@ def get_vlabels_values(self, start, stop):
20152005
index = self.data.index[start:stop]
20162006
if isinstance(index, pd.MultiIndex):
20172007
# returns a 1D array of tuples
2018-
return index.values
2008+
return index.to_numpy()
20192009
else:
2020-
return index.values[:, np.newaxis]
2010+
return index.to_numpy()[:, np.newaxis]
20212011

20222012
def get_hlabels_values(self, start, stop):
20232013
return [['']]
20242014

20252015
def get_values(self, h_start, v_start, h_stop, v_stop):
20262016
assert h_start == 0
2027-
return self.data.iloc[v_start:v_stop].values.reshape(-1, 1)
2017+
return self.data.iloc[v_start:v_stop].to_numpy().reshape(-1, 1)
20282018

20292019

20302020
@adapter_for('pandas.core.groupby.generic.DataFrameGroupBy')

0 commit comments

Comments
 (0)