Skip to content

Instantly share code, notes, and snippets.

@ikatson
Created November 28, 2013 01:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ikatson/7686087 to your computer and use it in GitHub Desktop.
Save ikatson/7686087 to your computer and use it in GitHub Desktop.
pandas bug: Series become empty somehow.
import pandas
class AggregatedValue(object):
def __init__(self, aggfunc, value=None, original=None):
self.aggfunc = aggfunc
self.value = value
self.original = original
def __repr__(self):
return '<AggVal: %r>' % self.value
def __call__(self, series):
value = self.aggfunc(series)
return self.__class__(self.aggfunc, value=value, original=series)
class AggregatedValueConvertSeriesToList(AggregatedValue):
def __call__(self, series):
series = list(series)
return super(AggregatedValueConvertSeriesToList, self).__call__(series)
idx = range(5)
d = {'date' : pandas.Series(('2011', '2011', '2011', '2012', '2013'), index=idx),
'manager' : pandas.Series(['m1', 'm2', 'm3', 'm1', 'm2'], index=idx),
'value' : pandas.Series([1, 1, 1, 2, 1], index=idx)}
df = pandas.DataFrame(d)
# The lengths of the original series here is 0 - it gets truncated.
result = df.pivot_table(
rows='date', cols='manager', values='value', aggfunc=AggregatedValue(len),
margins=False)
print 'Lengths of "originals"'
for value in result['m1']:
print len(getattr(value, 'original', []))
# The lengths of the original series here is not 0 any more, cause we converted it to list.
result = df.pivot_table(rows='date', cols='manager', values='value',
aggfunc=AggregatedValueConvertSeriesToList(len),
margins=False)
print 'Lengths of "originals", if series was converted to lists'
for value in result['m1']:
print len(getattr(value, 'original', []))
@ikatson
Copy link
Author

ikatson commented Nov 28, 2013

This outputs:

Lengths of "originals"
0 # Here, it should be 1
0 # Here, it should be 1
0
Lengths of "originals", if series was converted to lists
1
1
0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment