Created
November 28, 2013 01:45
-
-
Save ikatson/7686087 to your computer and use it in GitHub Desktop.
pandas bug: Series become empty somehow.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', [])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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