pandas 0.10.1 documentation

Frequently Asked Questions (FAQ)


Pandas is a powerful tool and already has a plethora of data manipulation operations implemented, most of them are very fast as well. It’s very possible however that certain functionality that would make your life easier is missing. In that case you have several options:

  1. Open an issue on Github , explain your need and the sort of functionality you would like to see implemented.

  2. Fork the repo, Implement the functionality yourself and open a PR on Github.

  3. Write a method that performs the operation you are interested in and Monkey-patch the pandas class as part of your IPython profile startup or PYTHONSTARTUP file.

    For example, here is an example of adding an just_foo_cols() method to the dataframe class:

In [452]: import pandas as pd

In [453]: def just_foo_cols(self):
   .....:     """Get a list of column names containing the string 'foo'
   .....:     """
   .....:     return [x for x in self.columns if 'foo' in x]
   .....:

In [454]: pd.DataFrame.just_foo_cols = just_foo_cols # monkey-patch the DataFrame class

In [455]: df = pd.DataFrame([range(4)],columns= ["A","foo","foozball","bar"])

In [456]: df.just_foo_cols()
Out[456]: ['foo', 'foozball']

In [457]: del pd.DataFrame.just_foo_cols # you can also remove the new method

Monkey-patching is usually frowned upon because it makes your code less portable and can cause subtle bugs in some circumstances. Monkey-patching existing methods is usually a bad idea in that respect. When used with proper care, however, it’s a very useful tool to have.

Migrating from scikits.timeseries to pandas >= 0.8.0

Starting with pandas 0.8.0, users of scikits.timeseries should have all of the features that they need to migrate their code to use pandas. Portions of the scikits.timeseries codebase for implementing calendar logic and timespan frequency conversions (but not resampling, that has all been implemented from scratch from the ground up) have been ported to the pandas codebase.

The scikits.timeseries notions of Date and DateArray are responsible for implementing calendar logic:

In [16]: dt = ts.Date('Q', '1984Q3')

# sic
In [17]: dt
Out[17]: <Q-DEC : 1984Q1>

In [18]: dt.asfreq('D', 'start')
Out[18]: <D : 01-Jan-1984>

In [19]: dt.asfreq('D', 'end')
Out[19]: <D : 31-Mar-1984>

In [20]: dt + 3
Out[20]: <Q-DEC : 1984Q4>

Date and DateArray from scikits.timeseries have been reincarnated in pandas Period and PeriodIndex:

In [458]: pnow('D')  # scikits.timeseries.now()
Out[458]: Period('2013-04-03', 'D')

In [459]: Period(year=2007, month=3, day=15, freq='D')
Out[459]: Period('2007-03-15', 'D')

In [460]: p = Period('1984Q3')

In [461]: p
Out[461]: Period('1984Q3', 'Q-DEC')

In [462]: p.asfreq('D', 'start')
Out[462]: Period('1984-07-01', 'D')

In [463]: p.asfreq('D', 'end')
Out[463]: Period('1984-09-30', 'D')

In [464]: (p + 3).asfreq('T') + 6 * 60 + 30
Out[464]: Period('1985-07-01 06:29', 'T')

In [465]: rng = period_range('1990', '2010', freq='A')

In [466]: rng
Out[466]: 
<class 'pandas.tseries.period.PeriodIndex'>
freq: A-DEC
[1990, ..., 2010]
length: 21

In [467]: rng.asfreq('B', 'end') - 3
Out[467]: 
<class 'pandas.tseries.period.PeriodIndex'>
freq: B
[1990-12-26, ..., 2010-12-28]
length: 21
scikits.timeseries pandas Notes
Date Period A span of time, from yearly through to secondly
DateArray PeriodIndex An array of timespans
convert resample Frequency conversion in scikits.timeseries
convert_to_annual pivot_annual currently supports up to daily frequency, see issue 736

PeriodIndex / DateArray properties and functions

The scikits.timeseries DateArray had a number of information properties. Here are the pandas equivalents:

scikits.timeseries pandas Notes
get_steps np.diff(idx.values)  
has_missing_dates not idx.is_full  
is_full idx.is_full  
is_valid idx.is_monotonic and idx.is_unique  
is_chronological is_monotonic  
arr.sort_chronologically() idx.order()  

Frequency conversion

Frequency conversion is implemented using the resample method on TimeSeries and DataFrame objects (multiple time series). resample also works on panels (3D). Here is some code that resamples daily data to montly with scikits.timeseries:

In [468]: import scikits.timeseries as ts
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-468-a44de061a6d8> in <module>()
----> 1 import scikits.timeseries as ts
ImportError: No module named scikits.timeseries

In [469]: data = ts.time_series(np.random.randn(50), start_date='Jan-2000', freq='M')
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-469-c16b12c482e8> in <module>()
----> 1 data = ts.time_series(np.random.randn(50), start_date='Jan-2000', freq='M')
AttributeError: 'TimeSeries' object has no attribute 'time_series'

In [470]: data
Out[470]: 
{'Label1': <class 'pandas.core.panel.Panel'>
Dimensions: 1 (items) x 4 (major_axis) x 3 (minor_axis)
Items axis: Item1 to Item1
Major_axis axis: 0 to 3
Minor_axis axis: 0 to 2,
 'Label2': <class 'pandas.core.panel.Panel'>
Dimensions: 1 (items) x 4 (major_axis) x 2 (minor_axis)
Items axis: Item2 to Item2
Major_axis axis: 0 to 3
Minor_axis axis: 0 to 1}

In [471]: data.convert('A', func=np.mean)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-471-42ee10087ca8> in <module>()
----> 1 data.convert('A', func=np.mean)
AttributeError: 'dict' object has no attribute 'convert'

Here is the equivalent pandas code:

In [472]: rng = period_range('Jan-2000', periods=50, freq='M')

In [473]: data = Series(np.random.randn(50), index=rng)

In [474]: data
Out[474]: 
2000-01    0.469112
2000-02   -0.282863
2000-03   -1.509059
2000-04   -1.135632
2000-05    1.212112
2000-06   -0.173215
2000-07    0.119209
2000-08   -1.044236
2000-09   -0.861849
2000-10   -2.104569
2000-11   -0.494929
2000-12    1.071804
2001-01    0.721555
2001-02   -0.706771
2001-03   -1.039575
2001-04    0.271860
2001-05   -0.424972
2001-06    0.567020
2001-07    0.276232
2001-08   -1.087401
2001-09   -0.673690
2001-10    0.113648
2001-11   -1.478427
2001-12    0.524988
2002-01    0.404705
2002-02    0.577046
2002-03   -1.715002
2002-04   -1.039268
2002-05   -0.370647
2002-06   -1.157892
2002-07   -1.344312
2002-08    0.844885
2002-09    1.075770
2002-10   -0.109050
2002-11    1.643563
2002-12   -1.469388
2003-01    0.357021
2003-02   -0.674600
2003-03   -1.776904
2003-04   -0.968914
2003-05   -1.294524
2003-06    0.413738
2003-07    0.276662
2003-08   -0.472035
2003-09   -0.013960
2003-10   -0.362543
2003-11   -0.006154
2003-12   -0.923061
2004-01    0.895717
2004-02    0.805244
Freq: M

In [475]: data.resample('A', how=np.mean)
Out[475]: 
2000   -0.394510
2001   -0.244628
2002   -0.221633
2003   -0.453773
2004    0.850481
Freq: A-DEC

Plotting

Much of the plotting functionality of scikits.timeseries has been ported and adopted to pandas’s data structures. For example:

In [476]: rng = period_range('1987Q2', periods=10, freq='Q-DEC')

In [477]: data = Series(np.random.randn(10), index=rng)

In [478]: plt.figure(); data.plot()
Out[478]: <matplotlib.axes.AxesSubplot at 0xb8ecf6c>
_images/skts_ts_plot.png

Converting to and from period format

Use the to_timestamp and to_period instance methods.

Treatment of missing data

Unlike scikits.timeseries, pandas data structures are not based on NumPy’s MaskedArray object. Missing data is represented as NaN in numerical arrays and either as None or NaN in non-numerical arrays. Implementing a version of pandas’s data structures that use MaskedArray is possible but would require the involvement of a dedicated maintainer. Active pandas developers are not interested in this.

Resampling with timestamps and periods

resample has a kind argument which allows you to resample time series with a DatetimeIndex to PeriodIndex:

In [479]: rng = date_range('1/1/2000', periods=200, freq='D')

In [480]: data = Series(np.random.randn(200), index=rng)

In [481]: data[:10]
Out[481]: 
2000-01-01   -0.076467
2000-01-02   -1.187678
2000-01-03    1.130127
2000-01-04   -1.436737
2000-01-05   -1.413681
2000-01-06    1.607920
2000-01-07    1.024180
2000-01-08    0.569605
2000-01-09    0.875906
2000-01-10   -2.211372
Freq: D

In [482]: data.index
Out[482]: 
<class 'pandas.tseries.index.DatetimeIndex'>
[2000-01-01 00:00:00, ..., 2000-07-18 00:00:00]
Length: 200, Freq: D, Timezone: None

In [483]: data.resample('M', kind='period')
Out[483]: 
2000-01   -0.175775
2000-02    0.094874
2000-03    0.124949
2000-04    0.066215
2000-05   -0.040364
2000-06    0.116263
2000-07   -0.263235
Freq: M

Similarly, resampling from periods to timestamps is possible with an optional interval ('start' or 'end') convention:

In [484]: rng = period_range('Jan-2000', periods=50, freq='M')

In [485]: data = Series(np.random.randn(50), index=rng)

In [486]: resampled = data.resample('A', kind='timestamp', convention='end')

In [487]: resampled.index
Out[487]: 
<class 'pandas.tseries.index.DatetimeIndex'>
[2000-12-31 00:00:00, ..., 2004-12-31 00:00:00]
Length: 5, Freq: A-DEC, Timezone: None