• Stars
    star
    196
  • Rank 198,553 (Top 4 %)
  • Language
    HTML
  • License
    Other
  • Created about 10 years ago
  • Updated over 9 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

functional data manipulation for pandas

pandas-ply: functional data manipulation for pandas

pandas-ply is a thin layer which makes it easier to manipulate data with pandas. In particular, it provides elegant, functional, chainable syntax in cases where pandas would require mutation, saved intermediate values, or other awkward constructions. In this way, it aims to move pandas closer to the "grammar of data manipulation" provided by the dplyr package for R.

For example, take the dplyr code below:

flights %>%
  group_by(year, month, day) %>%
  summarise(
    arr = mean(arr_delay, na.rm = TRUE),
    dep = mean(dep_delay, na.rm = TRUE)
  ) %>%
  filter(arr > 30 & dep > 30)

The most common way to express this in pandas is probably:

grouped_flights = flights.groupby(['year', 'month', 'day'])
output = pd.DataFrame()
output['arr'] = grouped_flights.arr_delay.mean()
output['dep'] = grouped_flights.dep_delay.mean()
filtered_output = output[(output.arr > 30) & (output.dep > 30)]

pandas-ply lets you instead write:

(flights
  .groupby(['year', 'month', 'day'])
  .ply_select(
    arr = X.arr_delay.mean(),
    dep = X.dep_delay.mean())
  .ply_where(X.arr > 30, X.dep > 30))

In our opinion, this pandas-ply code is cleaner, more expressive, more readable, more concise, and less error-prone than the original pandas code.

Explanatory notes on the pandas-ply code sample above:

  • pandas-ply's methods (like ply_select and ply_where above) are attached directly to pandas objects and can be used immediately, without any wrapping or redirection. They start with a ply_ prefix to distinguish them from built-in pandas methods.
  • pandas-ply's methods are named for (and modelled after) SQL's operators. (But keep in mind that these operators will not always appear in the same order as they do in a SQL statement: SELECT a FROM b WHERE c GROUP BY d probably maps to b.ply_where(c).groupby(d).ply_select(a).)
  • pandas-ply includes a simple system for building "symbolic expressions" to provide as arguments to its methods. X above is an instance of ply.symbolic.Symbol. Operations on this symbol produce larger compound symbolic expressions. When pandas-ply receives a symbolic expression as an argument, it converts it into a function. So, for instance, X.arr > 30 in the above code could have instead been provided as lambda x: x.arr > 30. Use of symbolic expressions allows the lambda x: to be left off, resulting in less cluttered code.

Warning

pandas-ply is new, and in an experimental stage of its development. The API is not yet stable. Expect the unexpected.

(Pull requests are welcome. Feel free to contact us at [email protected].)

Using pandas-ply

Install pandas-ply with:

$ pip install pandas-ply

Typical use of pandas-ply starts with:

import pandas as pd
from pandas_ply import install_ply, X, sym_call

install_ply(pd)

After calling install_ply, all pandas objects have pandas-ply's methods attached.

API reference

Full API reference is available at http://pythonhosted.org/pandas-ply/.

Possible TODOs

  • Extend pandas' native groupby to support symbolic expressions?
  • Extend pandas' native apply to support symbolic expressions?
  • Add .ply_call to pandas objects to extend chainability?
  • Version of ply_select which supports later computed columns relying on earlier computed columns?
  • Version of ply_select which supports careful column ordering?
  • Better handling of indices?

License

Copyright 2015 Coursera Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

More Repositories

1

dataduct

DataPipeline for humans.
Python
249
star
2

naptime

Making REST APIs so easy, you can build them in your sleep!
Scala
133
star
3

courier

Data interchange for the modern web + mobile stack.
Scala
98
star
4

autoschema

Generates JSON Schema from Scala classes
Scala
53
star
5

retracked

Event tracking for React components
JavaScript
49
star
6

js-libraries-snapshot

A snapshot of a few JavaScript libraries used in the Coursera frontend stack.
JavaScript
34
star
7

programming-assignments-demo

Demos for instructors.
Python
32
star
8

courseraprogramming

Command line tools to help instructional teams develop, deploy, and debug sophisticated asynchronous graders.
Python
31
star
9

swift-style-guide

Coursera's Swift Style Guide
30
star
10

shift

Shift is a development tool that allows you to modify variables and run blocks of code while your Android application is running.
Java
24
star
11

courseraresearchexports

Python library and tooling for Coursera Research Exports
Python
19
star
12

bootstrap3-stylus

CSS
16
star
13

forum-js-snapshot

A snapshot of the JS used to power the new Coursera forums - views, models, templates, and tests.
15
star
14

coursera-labs

JavaScript
14
star
15

ios-scrubber

Swift
14
star
16

yudi

jade i18n module
JavaScript
13
star
17

coursera_autograder

Python
12
star
18

react-imgix

Isomorphic React component for images scaling and manipulation using Imgix
JavaScript
12
star
19

plugin-examples

examples of plugins for use by Coursera partners
JavaScript
12
star
20

courscala

Core Scala classes used to build Coursera.
Scala
10
star
21

buggy

express router app, providing a slack integration for creating, searching, retrieving and assigning jira issues
JavaScript
9
star
22

foody

slack command for managing your organization's internal food menu
JavaScript
7
star
23

booky

slack command for searching gitbooks using algolia
JavaScript
6
star
24

coursera.github.io

Opensource @ Coursera
CSS
4
star
25

scribe-plugin-table-command

Table command for scribe plugins.
JavaScript
4
star
26

courseraoauth2client

Python library for accessing Coursera resources protected by OAuth 2.0
Python
4
star
27

COShakit-ios

Objective-C
4
star
28

emscripts

JavaScript
2
star
29

COShakit-android

Java
2
star
30

eslint-config-coursera

The ESLint configuration we use at Coursera
JavaScript
2
star
31

gitbook-plugin-page-feedback

Page feedback for gitbook, recorded in a google spreadsheet and routed to slack
JavaScript
2
star
32

fontawesome4-stylus

a fontawesome port for stylus (a css processor)
CSS
1
star
33

scribe-plugins

1
star
34

triple-latte

mochajs with extra caffeine (parallel execution, modified xunit reporter and moar)
JavaScript
1
star
35

abcdefghiJson

Print your JSON alphabetized!
JavaScript
1
star
36

eslint-plugin-coursera

A place to hold eslint rules used by Coursera developers.
JavaScript
1
star