Question

For some reason, the simple last operation is not working for my dataframe:

df
Out[57]: 


   month     date      value
0 2013-01-01 2013-01-25 0.0223
1 2013-01-01 2013-01-28 0.0006
2 2013-01-01 2013-01-29 0.0071
3 2013-01-01 2013-01-30 0.0062
4 2013-01-01 2013-01-31 0.0037
5 2013-02-01 2013-02-01 0.0151
6 2013-02-01 2013-02-04 0.012
7 2013-02-01 2013-02-05 0.0181
8 2013-02-01 2013-02-06 -0.0075
9 2013-02-01 2013-02-07 -0.0057

10 rows × 3 columns 

df.groupby('month').last()
Out[58]: 


           date      value
month
2013-01-01 2013-01-01 2013-01-01
2013-02-01 2013-02-01 2013-02-01

2 rows × 2 columns 

df.dtypes
Out[59]: 
month    datetime64[ns]
date     datetime64[ns]
value            object
dtype: object

I am using pandas 13.1. Is this a new bug?

Was it helpful?

Solution

This is a bug in 0.13.1. Fixed in master/0.14 (releasing shortly). Also in 0.14 this will coerce the value column to float64 (you have it as object for some reason; never a good thing with a float-like column).

Here's a work-around for 0.13.1 (the extra month column is also going away in 0.14).

In [14]: df.groupby('month').tail(1)
Out[14]: 
                  month       date   value
month                                     
2013-01-01 4 2013-01-01 2013-01-31  0.0037
2013-02-01 9 2013-02-01 2013-02-07 -0.0057

[2 rows x 3 columns]

Here's 0.14/master output

In [32]: df.groupby('month').last()
Out[32]: 
                 date   value
month                        
2013-01-01 2013-01-31  0.0037
2013-02-01 2013-02-07 -0.0057

In [33]: df.groupby('month').last().dtypes
Out[33]: 
date     datetime64[ns]
value           float64
dtype: object
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top