Skip to content

Commit 34c508b

Browse files
authored
Move path import, update docs (#7)
* Move path import and fix empty string bug * Add quickstart * Add history, update version
1 parent 3729a74 commit 34c508b

File tree

6 files changed

+56
-9
lines changed

6 files changed

+56
-9
lines changed

HISTORY.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# History
2+
3+
## v0.3.0 (2021-03-14)
4+
5+
- Added HISTORY.md file
6+
- Changed import syntax to `from pandas_path import path` to better support custom accessors. This means that we do not register `.path` unless you import `.path` so that in other libraries you don't have to add the `.path` accessor if you are just registering your own custom accessor.
7+
- Added quickstart section to README
8+
- Fix bug where we tried to instantiate with blank string for an example rather than first item in the Series

README.md

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,42 @@ This package is for you. Just one import adds a `.path` accessor to any pandas S
1010

1111
<small> * If not, you should.</small>
1212

13+
## Quickstart
14+
15+
Install latest `pandas-path` with `pip`.
16+
17+
```bash
18+
pip install pandas-path
19+
```
20+
21+
Import `path` from `pandas_path`, and then the `.path` accessor will be available on any Series or Index:
22+
23+
```python
24+
# this is all you need
25+
from pandas_path import path
26+
```
27+
28+
Now you can use all the pathlib methods using the `.path` accessor on any Series in `pandas`!
29+
30+
```python
31+
pd.Series([
32+
'cat/1.jpg',
33+
'cat/2.jpg',
34+
'dog/1.jpg',
35+
'dog/2.jpg',
36+
]).path.parent
37+
38+
# 0 cat
39+
# 1 cat
40+
# 2 dog
41+
# 3 dog
42+
# dtype: object
43+
```
44+
45+
46+
## Examples
47+
48+
1349
Here's an example:
1450

1551
```python
@@ -18,7 +54,7 @@ import pandas as pd
1854

1955
# This is the only line you need to register `.path` as an accessor
2056
# on any Series or Index in pandas.
21-
import pandas_path
57+
from pandas_path import path
2258

2359
# we'll make an example series from the py files in this repo;
2460
# note that every element here is just a string--no need to make Path objects yourself

pandas_path/accessor.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from pathlib import Path
21
from types import FunctionType, LambdaType, MethodType
32

43
import numpy as np
@@ -42,11 +41,12 @@ def __getattr__(self, attr):
4241

4342
apply_series = self._obj.to_series() if isinstance(self._obj, pd.Index) else self._obj
4443

45-
# check the type of this attribute on a Path object
44+
# check the type of this attribute on a Path object (we need an actual instance) since
45+
# the super classes dispatch
4646
if isinstance(self._obj.values[0], path_class):
4747
attr_type = getattr(type(self._obj.values[0]), attr, None)
4848
else:
49-
attr_type = getattr(type(self._to_path_object("")), attr, None)
49+
attr_type = getattr(type(self._to_path_object(self._obj.values[0])), attr, None)
5050

5151
# if we're asking for a property, do the calculation and return the result
5252
if isinstance(attr_type, property):
@@ -137,7 +137,3 @@ def register_path_accessor(accessor_name, path_class, *args, **kwargs):
137137

138138
pd.api.extensions.register_series_accessor(accessor_name)(accessor_class)
139139
pd.api.extensions.register_index_accessor(accessor_name)(accessor_class)
140-
141-
142-
# default to registering `Path` to `path`
143-
register_path_accessor("path", Path)

pandas_path/path/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from pathlib import Path
2+
3+
from ..accessor import register_path_accessor # noqa
4+
5+
6+
register_path_accessor("path", Path)

pandas_path/tests.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ def pd():
2424
@pytest.fixture
2525
def pandas_path():
2626
import pandas_path
27+
from pandas_path import path # noqa
2728

2829
assert pandas_path.__version__ is not None
2930

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def load_reqs(path):
3131
"Source Code": "https://github.com/drivendataorg/pandas-path",
3232
"DrivenData": "http://drivendata.co",
3333
},
34-
version="0.2.0",
34+
version="0.3.0",
3535
author="DrivenData",
3636
author_email="[email protected]",
3737
include_package_data=True,

0 commit comments

Comments
 (0)