Skip to content

Commit 43fbd2d

Browse files
committed
Allow selecting based on dt.day, etc. comparisons
1 parent 4ba2f72 commit 43fbd2d

File tree

1 file changed

+35
-25
lines changed

1 file changed

+35
-25
lines changed

pandas_selectable/__init__.py

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import operator
33
from functools import wraps
44

5+
import numpy as np
56
import pandas as pd
67
from pandas.core.accessor import CachedAccessor
78
from pandas.core.indexes.accessors import (
@@ -70,31 +71,6 @@ def _wrap_result(self, *args, **kwargs):
7071
return self._parent_frame.loc[bool_idx]
7172

7273

73-
class SelectDatetimeProperties(DatetimeProperties):
74-
def __init__(self, parent, *args, **kwargs):
75-
# datetime properties holds an attribute _parent
76-
# we need to add the parent_frame (or series) to the subclass instances
77-
self._parent_frame = parent
78-
super().__init__(*args, **kwargs)
79-
80-
def __getattribute__(self, attr):
81-
if (
82-
not attr.startswith("_")
83-
and inspect.isroutine( # noqa
84-
getattr(DatetimeProperties, attr, None)
85-
)
86-
and attr not in _date_boolean_methods
87-
): # noqa
88-
raise NotImplementedError(
89-
"Boolean selection with this method " "does not make sense."
90-
)
91-
elif attr in _date_boolean_methods:
92-
idx = super().__getattribute__(attr)
93-
return self._parent_frame.loc[idx]
94-
else:
95-
return super().__getattribute__(attr)
96-
97-
9874
class SelectPeriodProperties(PeriodProperties):
9975
def __init__(self, parent, *args, **kwargs):
10076
self._parent_frame = parent
@@ -252,3 +228,37 @@ def __getitem__(self, key):
252228
@property
253229
def index(self):
254230
return SelectableIndex(self._frame)
231+
232+
233+
class SelectDatetimeProperties(DatetimeProperties):
234+
def __init__(self, parent, *args, **kwargs):
235+
# datetime properties holds an attribute _parent
236+
# we need to add the parent_frame (or series) to the subclass instances
237+
self._parent_frame = parent
238+
super().__init__(*args, **kwargs)
239+
240+
def __getattribute__(self, attr):
241+
if (
242+
not attr.startswith("_")
243+
and inspect.isroutine( # noqa
244+
getattr(DatetimeProperties, attr, None)
245+
)
246+
and attr not in _date_boolean_methods
247+
): # noqa
248+
raise NotImplementedError(
249+
"Boolean selection with this method " "does not make sense."
250+
)
251+
elif attr in _date_boolean_methods:
252+
idx = super().__getattribute__(attr)
253+
return self._parent_frame.loc[idx]
254+
else:
255+
got_attr = super().__getattribute__(attr)
256+
# this allows things like dt.day, dt.month to be selectable
257+
# for the parent frame. assumes they're all properties.
258+
if (
259+
isinstance(got_attr, pd.Series)
260+
and not attr.startswith('_')
261+
and isinstance(getattr(self.__class__, attr), property)
262+
):
263+
return SelectableColumn(self._parent_frame, got_attr)
264+
return got_attr

0 commit comments

Comments
 (0)