|
1 | 1 | # Pandas Flavor |
2 | | -**Easily write your own flavor of Pandas** |
| 2 | +**The easy way to write your own flavor of Pandas** |
3 | 3 |
|
4 | | -Pandas added `register_series_accessor` and `register_dataframe_accessor` decorators |
5 | | -in 0.23. These extensions allow you to easily add new accessors to Pandas objects that |
6 | | -are persistent. This means, you can easily write your own flavor of the DataFrame. |
| 4 | +Pandas added an new (simple) API to register accessors with Pandas objects. |
| 5 | +This package adds support for registering methods as well. |
7 | 6 |
|
8 | | -To see an example, checkout [pdvega](https://github.com/jakevdp/pdvega). This library adds a new Vega plotting accessor |
9 | | -under the `vgplot` attribute and mirror the `plot` (matplotlib-based) accessor. |
| 7 | +*What does this mean?* |
10 | 8 |
|
11 | | -**Pandas Flavor** takes this extension module a step further and adds similar syntax |
12 | | -for registering new methods! |
| 9 | +It's super easy to custom functionality to Pandas DataFrames and Series. Import |
| 10 | +this package. Write simple python function. Register it using one of the following |
| 11 | +decorators. |
13 | 12 |
|
14 | | - |
| 13 | +*Why?* |
15 | 14 |
|
16 | | -To see another example, check out [PhyloPandas](https://github.com/Zsailer/phylopandas). |
| 15 | +Pandas is super handy. It's general purpose is to be a "flexible and powerful data analysis / manipulation library". With this API, you can tailor pandas to fit a specific |
| 16 | +field or use case. |
| 17 | + |
| 18 | +Maybe you want to a DataFrame that loads your specific data (and checks if the file |
| 19 | + contains unwanted columns)? |
| 20 | + |
| 21 | +Maybe you want custom plot functions? |
| 22 | + |
| 23 | +Maybe something else? |
| 24 | + |
| 25 | +## Register accessors |
| 26 | + |
| 27 | +Accessors (in pandas) are objects attached to a attribute on the Pandas DataFrame/Series |
| 28 | +that provide extra, specific functionality. For example, `pandas.DataFrame.plot` is an |
| 29 | +accessor that provides plotting functionality. |
| 30 | + |
| 31 | +Add an accessor by registering the function with the following decorator |
| 32 | +and passing the decorator an accessor name. |
| 33 | + |
| 34 | +```python |
| 35 | +import pandas as pd |
| 36 | +import pandas_flavor as pf |
| 37 | + |
| 38 | +@pf.register_dataframe_accessor('my_flavor') |
| 39 | +def is_cool(df): |
| 40 | + """Is my accessor cool?""" |
| 41 | + return True |
| 42 | + |
| 43 | +# DataFrame. |
| 44 | +df = DataFrame() |
| 45 | + |
| 46 | +# Access this functionality |
| 47 | +df.my_flavor.is_cool() |
| 48 | +# Returns True |
| 49 | +True |
| 50 | +``` |
| 51 | + |
| 52 | +To see this in action, check out [pdvega](https://github.com/jakevdp/pdvega)! this |
| 53 | +library adds a new plotting accessor for making Vega plots from pandas data. |
| 54 | + |
| 55 | +## Register methods |
| 56 | + |
| 57 | +Using this package, you can attach functions directly to Pandas objects. No |
| 58 | +intermediate accessor is needed. |
| 59 | + |
| 60 | +```python |
| 61 | +import pandas as pd |
| 62 | +import pandas_flavor as pf |
| 63 | + |
| 64 | +@pf.register_dataframe_method |
| 65 | +def is_cool(df): |
| 66 | + """Is my dataframe cool?""" |
| 67 | + return True |
| 68 | + |
| 69 | +# DataFrame. |
| 70 | +df = DataFrame() |
| 71 | + |
| 72 | +# Access this functionality |
| 73 | +df.is_cool() |
| 74 | +# Returns True |
| 75 | +True |
| 76 | +``` |
| 77 | + |
| 78 | +To see in action, check out [PhyloPandas](https://github.com/Zsailer/phylopandas). |
17 | 79 | This library adds extra `to_` methods for writing DataFrames to various biological |
18 | 80 | sequencing file formats. |
0 commit comments