All Projects → Zsailer → Pandas_flavor

Zsailer / Pandas_flavor

Licence: mit
The easy way to write your own flavor of Pandas

Programming Languages

python
139335 projects - #7 most used programming language

Labels

Projects that are alternatives of or similar to Pandas flavor

Ta
Technical Analysis Library using Pandas and Numpy
Stars: ✭ 2,649 (+1217.91%)
Mutual labels:  pandas
Andrew Ng Notes
This is Andrew NG Coursera Handwritten Notes.
Stars: ✭ 180 (-10.45%)
Mutual labels:  pandas
Fashion Recommendation
A clothing retrieval and visual recommendation model for fashion images.
Stars: ✭ 193 (-3.98%)
Mutual labels:  pandas
Xarray
N-D labeled arrays and datasets in Python
Stars: ✭ 2,353 (+1070.65%)
Mutual labels:  pandas
Tensorflow Ml Nlp
텐서플로우와 머신러닝으로 시작하는 자연어처리(로지스틱회귀부터 트랜스포머 챗봇까지)
Stars: ✭ 176 (-12.44%)
Mutual labels:  pandas
Choochoo
Training Diary
Stars: ✭ 186 (-7.46%)
Mutual labels:  pandas
Learnpythonforresearch
This repository provides everything you need to get started with Python for (social science) research.
Stars: ✭ 163 (-18.91%)
Mutual labels:  pandas
Arctic
High performance datastore for time series and tick data
Stars: ✭ 2,525 (+1156.22%)
Mutual labels:  pandas
Data Science Types
Mypy stubs, i.e., type information, for numpy, pandas and matplotlib
Stars: ✭ 180 (-10.45%)
Mutual labels:  pandas
Zebras
Data analysis library for JavaScript built with Ramda
Stars: ✭ 192 (-4.48%)
Mutual labels:  pandas
Panthera
Data-frames & arrays on Clojure
Stars: ✭ 168 (-16.42%)
Mutual labels:  pandas
Mars
Mars is a tensor-based unified framework for large-scale data computation which scales numpy, pandas, scikit-learn and Python functions.
Stars: ✭ 2,308 (+1048.26%)
Mutual labels:  pandas
Dtale
Visualizer for pandas data structures
Stars: ✭ 2,864 (+1324.88%)
Mutual labels:  pandas
Pdblp
pandas wrapper for Bloomberg Open API
Stars: ✭ 165 (-17.91%)
Mutual labels:  pandas
Finance
Here you can find all the quantitative finance algorithms that I've worked on and refined over the past year!
Stars: ✭ 194 (-3.48%)
Mutual labels:  pandas
Pandas Datareader
Extract data from a wide range of Internet sources into a pandas DataFrame.
Stars: ✭ 2,183 (+986.07%)
Mutual labels:  pandas
Pandasgui
PandasGUI is a GUI for viewing, plotting and analyzing Pandas DataFrames.
Stars: ✭ 2,495 (+1141.29%)
Mutual labels:  pandas
Data Science Projects With Python
A Case Study Approach to Successful Data Science Projects Using Python, Pandas, and Scikit-Learn
Stars: ✭ 198 (-1.49%)
Mutual labels:  pandas
Data Science Notebook
📖 每一个伟大的思想和行动都有一个微不足道的开始
Stars: ✭ 196 (-2.49%)
Mutual labels:  pandas
California Coronavirus Data
The Los Angeles Times' independent tally of coronavirus cases in California.
Stars: ✭ 188 (-6.47%)
Mutual labels:  pandas

Pandas Flavor

The easy way to write your own flavor of Pandas

Pandas 0.23 added a (simple) API for registering accessors with Pandas objects.

Pandas-flavor extends Pandas' extension API by:

  1. adding support for registering methods as well.
  2. making each of these functions backwards compatible with older versions of Pandas.

What does this mean?

It is now simpler to add custom functionality to Pandas DataFrames and Series.

Import this package. Write a simple python function. Register the function using one of the following decorators.

Why?

Pandas is super handy. Its general purpose is to be a "flexible and powerful data analysis/manipulation library".

Pandas Flavor allows you add functionality that tailors Pandas to specific fields or use cases.

Maybe you want to add new write methods to the Pandas DataFrame? Maybe you want custom plot functionality? Maybe something else?

Register accessors

Accessors (in pandas) are objects attached to a attribute on the Pandas DataFrame/Series that provide extra, specific functionality. For example, pandas.DataFrame.plot is an accessor that provides plotting functionality.

Add an accessor by registering the function with the following decorator and passing the decorator an accessor name.

# my_flavor.py

import pandas as pd
import pandas_flavor as pf

@pf.register_dataframe_accessor('my_flavor')
class MyFlavor(object):

  def __init__(self, data):
    self._data

    def row_by_value(self, col, value):
        """Slice out row from DataFrame by a value."""
        return self._data[self._data[col] == value].squeeze()

Every dataframe now has this accessor as an attribute.

import my_flavor

# DataFrame.
df = pd.DataFrame(data={
  "x": [10, 20, 25],
  "y": [0, 2, 5]
})

# Print DataFrame
print(df)

# x  y
# 0  10  0
# 1  20  2
# 2  25  5

# Access this functionality
df.my_flavor.row_by_value('x', 10)

# x    10
# y     0
# Name: 0, dtype: int64

To see this in action, check out pdvega, PhyloPandas, and pyjanitor!

Register methods

Using this package, you can attach functions directly to Pandas objects. No intermediate accessor is needed.

# my_flavor.py

import pandas as pd
import pandas_flavor as pf

@pf.register_dataframe_method
def row_by_value(df, col, value):
    """Slice out row from DataFrame by a value."""
    return df[df[col] == value].squeeze()

import my_flavor

# DataFrame.
df = DataFrame(data={
  "x": [10, 20, 25],
  "y": [0, 2, 5]
})

# Print DataFrame
print(df)

# x  y
# 0  10  0
# 1  20  2
# 2  25  5

# Access this functionality
df.row_by_value('x', 10)

# x    10
# y     0
# Name: 0, dtype: int64

Available Methods

  • register_dataframe_method: register a method directly with a pandas DataFrame.
  • register_dataframe_accessor: register an accessor (and it's methods) with a pandas DataFrame.
  • register_series_method: register a methods directly with a pandas Series.
  • register_series_accessor: register an accessor (and it's methods) with a pandas Series.

Installation

You can install using pip:

pip install pandas_flavor

or conda (thanks @ericmjl)!

conda install -c conda-forge pandas-flavor

Contributing

Pull requests are always welcome! If you find a bug, don't hestitate to open an issue or submit a PR. If you're not sure how to do that, check out this simple guide.

If you have a feature request, please open an issue or submit a PR!

TL;DR

Pandas 0.23 introduced a simpler API for extending Pandas. This API provided two key decorators, register_dataframe_accessor and register_series_accessor, that enable users to register accessors with Pandas DataFrames and Series.

Pandas Flavor originated as a library to backport these decorators to older versions of Pandas (<0.23). While doing the backporting, it became clear that registering methods directly to Pandas objects might be a desired feature as well.*

*It is likely that Pandas deliberately chose not implement to this feature. If everyone starts monkeypatching DataFrames with their custom methods, it could lead to confusion in the Pandas community. The preferred Pandas approach is to namespace your methods by registering an accessor that contains your custom methods.

So how does method registration work?

When you register a method, Pandas flavor actually creates and registers a (this is subtle, but important) custom accessor class that mimics the behavior of a method by:

  1. inheriting the docstring of your function
  2. overriding the __call__ method to call your function.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].