|
25 | 25 |
|
26 | 26 | import runmanager
|
27 | 27 |
|
| 28 | +# Monkey patch a bugfix onto older versions of pandas on Python 2. This code |
| 29 | +# can be removed once lyse otherwise depends on pandas >= 0.21.0. |
| 30 | +# https://github.com/pandas-dev/pandas/pull/17099 |
| 31 | +if six.PY2: |
| 32 | + try: |
| 33 | + from labscript_utils import check_version, VersionException |
| 34 | + check_version('pandas', '0.21.0', '2.0') |
| 35 | + except VersionException: |
| 36 | + |
| 37 | + import numpy as np |
| 38 | + from pandas import Series, Index |
| 39 | + from pandas.core.indexing import maybe_droplevels |
| 40 | + def _getitem_multilevel(self, key): |
| 41 | + loc = self.columns.get_loc(key) |
| 42 | + if isinstance(loc, (slice, Series, np.ndarray, Index)): |
| 43 | + new_columns = self.columns[loc] |
| 44 | + result_columns = maybe_droplevels(new_columns, key) |
| 45 | + if self._is_mixed_type: |
| 46 | + result = self.reindex(columns=new_columns) |
| 47 | + result.columns = result_columns |
| 48 | + else: |
| 49 | + new_values = self.values[:, loc] |
| 50 | + result = self._constructor(new_values, index=self.index, |
| 51 | + columns=result_columns) |
| 52 | + result = result.__finalize__(self) |
| 53 | + if len(result.columns) == 1: |
| 54 | + top = result.columns[0] |
| 55 | + if isinstance(top, tuple): |
| 56 | + top = top[0] |
| 57 | + if top == '': |
| 58 | + result = result[''] |
| 59 | + if isinstance(result, Series): |
| 60 | + result = self._constructor_sliced(result, |
| 61 | + index=self.index, |
| 62 | + name=key) |
| 63 | + |
| 64 | + result._set_is_copy(self) |
| 65 | + return result |
| 66 | + else: |
| 67 | + return self._get_item_cache(key) |
| 68 | + |
| 69 | + pandas.DataFrame._getitem_multilevel = _getitem_multilevel |
| 70 | + |
| 71 | + |
28 | 72 | def asdatetime(timestr):
|
29 | 73 | tz = tzlocal.get_localzone().zone
|
30 | 74 | return pandas.Timestamp(timestr, tz=tz)
|
|
0 commit comments