|
1 | 1 | """ Backend functions for pyspark.""" |
2 | 2 |
|
3 | | -import warnings |
4 | 3 | from functools import wraps |
5 | 4 |
|
6 | | -from janitor.utils import import_message |
7 | 5 |
|
| 6 | +try: |
| 7 | + from pyspark.pandas.extensions import register_dataframe_accessor |
8 | 8 |
|
9 | | -class CachedAccessor: |
10 | | - """ |
11 | | - Custom property-like object (descriptor) for caching accessors. |
12 | | -
|
13 | | - Parameters |
14 | | - ---------- |
15 | | - name : str |
16 | | - The namespace this will be accessed under, e.g. `df.foo` |
17 | | - accessor : cls |
18 | | - The class with the extension methods. |
19 | | -
|
20 | | - NOTE |
21 | | - ---- |
22 | | - Modified based on pandas.core.accessor. |
23 | | - """ |
24 | | - |
25 | | - def __init__(self, name, accessor): |
26 | | - self._name = name |
27 | | - self._accessor = accessor |
28 | | - |
29 | | - def __get__(self, obj, cls): |
30 | | - if obj is None: |
31 | | - # we're accessing the attribute of the class, i.e., Dataset.geo |
32 | | - return self._accessor |
33 | | - accessor_obj = self._accessor(obj) |
34 | | - # Replace the property with the accessor object. Inspired by: |
35 | | - # http://www.pydanny.com/cached-property.html |
36 | | - setattr(obj, self._name, accessor_obj) |
37 | | - return accessor_obj |
38 | | - |
39 | | - |
40 | | -def _register_accessor(name, cls): |
41 | | - """ |
42 | | - NOTE |
43 | | - ---- |
44 | | - Modified based on pandas.core.accessor. |
45 | | - """ |
46 | | - |
47 | | - def decorator(accessor): |
48 | | - if hasattr(cls, name): |
49 | | - warnings.warn( |
50 | | - "registration of accessor {!r} under name {!r} for type " |
51 | | - "{!r} is overriding a preexisting attribute with the same " |
52 | | - "name.".format(accessor, name, cls), |
53 | | - UserWarning, |
54 | | - stacklevel=2, |
55 | | - ) |
56 | | - setattr(cls, name, CachedAccessor(name, accessor)) |
57 | | - return accessor |
58 | | - |
59 | | - return decorator |
60 | | - |
61 | | - |
62 | | -def register_dataframe_accessor(name): |
63 | | - """ |
64 | | - NOTE |
65 | | - ---- |
66 | | - Modified based on pandas.core.accessor. |
67 | | -
|
68 | | - .. # noqa: DAR101 name |
69 | | - .. # noqa: DAR201 |
70 | | - """ |
71 | | - try: |
72 | | - from pyspark.sql import DataFrame |
73 | | - except ImportError: |
74 | | - import_message( |
75 | | - submodule="spark", |
76 | | - package="pyspark", |
77 | | - conda_channel="conda-forge", |
78 | | - pip_install=True, |
79 | | - ) |
| 9 | +except ImportError: |
| 10 | + from janitor.utils import import_message |
80 | 11 |
|
81 | | - return _register_accessor(name, DataFrame) |
| 12 | + import_message( |
| 13 | + submodule="spark", |
| 14 | + package="pyspark", |
| 15 | + conda_channel="conda-forge", |
| 16 | + pip_install=True, |
| 17 | + ) |
82 | 18 |
|
83 | 19 |
|
84 | 20 | def register_dataframe_method(method): |
|
0 commit comments