larray-project / larray

N-dimensional labelled arrays in Python
https://larray.readthedocs.io/
GNU General Public License v3.0
8 stars 6 forks source link

Array.reindex fails if adding labels of different type #909

Open gdementen opened 3 years ago

gdementen commented 3 years ago
>>> arr = ndtest("year=2020..2022")
>>> arr.reindex("year", [2020, 2022])
year  2020  2022
       0.0   2.0
>>> arr.reindex("year", [2020, "a", 2022, "b"])
ValueError: incompatible axes:
Axis([], 'year')
vs
Axis([], 'year')

Bonus point for the totally cryptic error message 😉

gdementen commented 3 years ago

As a workaround in this case, we can change the axis type to string then use strings to reindex then rechange the labels but that's kinda awful:

>>> arr.set_labels("year", str).reindex("year", ["2020", "a", "2022", "b"]).set_labels("year", {"2020": 2020, "2022": 2022})
year  2020    a  2022    b
       0.0  nan   2.0  nan
gdementen commented 3 years ago

related to #910

gdementen commented 1 month ago

With recent versions of larray (at least 0.34.6), this now silently returns a bad result, instead of the cryptic error message, which is much worse, so I just bumped priority.

>>> arr = ndtest("year=2020..2022")
>>> res = arr.reindex("year", [2020, "a", 2022, "b"])
>>> res
year  2020    a  2022    b
       nan  nan   nan  nan
>>> res.info
4
 year [4]: '2020' 'a' '2022' 'b'
dtype: float64
memory used: 32 bytes