From c5dec821a6a4ed477becb196d85e165e21cd6246 Mon Sep 17 00:00:00 2001 From: Kristen Thyng Date: Fri, 28 Apr 2023 15:51:15 -0500 Subject: [PATCH] test and fix for index being guessed --- cf_pandas/accessor.py | 19 +++++++++++++------ tests/test_accessor.py | 11 +++++++++++ 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/cf_pandas/accessor.py b/cf_pandas/accessor.py index 0577f39..d2cfdd8 100644 --- a/cf_pandas/accessor.py +++ b/cf_pandas/accessor.py @@ -69,7 +69,9 @@ def _validate(self): # verify that necessary keys are present. Z would also be nice but might be missing. # but don't use the accessor to check keys = ["T", "longitude", "latitude"] - missing_keys = [key for key in keys if len(_get_axis_coord(self._obj, key)) == 0] + missing_keys = [ + key for key in keys if len(_get_axis_coord(self._obj, key)) == 0 + ] if len(missing_keys) > 0: raise AttributeError( f'{"longitude", "latitude", "time"} must be identifiable in DataFrame but {missing_keys} are missing.' @@ -368,14 +370,19 @@ def _get_axis_coord(obj: Union[DataFrame, Series], key: str) -> list: # units = getattr(col.data, "units", None) # if units in expected: # results.update((col,)) - # also use the guess_regex approach by default, but only if no results so far # this takes the logic from cf-xarray guess_coord_axis if len(results) == 0: - if key in ("T", "time") and _is_datetime_like(obj[col]): - results.update((col,)) - continue # prevent second detection - + if col in obj.columns: + if key in ("T", "time") and _is_datetime_like(obj[col]): + results.update((col,)) + continue # prevent second detection + elif col in obj.index.names: + if key in ("T", "time") and _is_datetime_like( + obj.index.get_level_values(col) + ): + results.update((col,)) + continue # prevent second detection pattern = guess_regex[key] if pattern.match(col.lower()): results.update((col,)) diff --git a/tests/test_accessor.py b/tests/test_accessor.py index 360b564..4351b30 100644 --- a/tests/test_accessor.py +++ b/tests/test_accessor.py @@ -128,3 +128,14 @@ def test_get_by_guess_regex(): assert df.cf["longitude"].name == "lon" assert df.cf["latitude"].name == "lat" assert df.cf["time"].name == "min" + + df = pd.DataFrame(columns=["blah_lon", "table_lat"]) + assert df.cf["longitude"].name == "blah_lon" + assert df.cf["latitude"].name == "table_lat" + + +def test_index(): + """Test when time is in index.""" + df = pd.DataFrame(index=["m_time"]) + df.index.rename("m_time", inplace=True) + assert df.cf["T"].name == "m_time"