Groupby aggregation of date/datetime columns returns datetime64 rather than numeric type #11444

Closed
michaelbilow opened this Issue Oct 27, 2015 · 1 comment

Comments

Projects
None yet
2 participants
import pandas as pd
import datetime
u = [datetime.datetime(2015, x, 1) for x in range(12)]
v = list('aaabbbbbbccd')
df = pd.DataFrame('X':v, 'Y':u)
df.groupby('X')['Y'].agg(len)
## Returns the following:
X
a   1970-01-01 00:00:00.000000003
b   1970-01-01 00:00:00.000000006
c   1970-01-01 00:00:00.000000002
d   1970-01-01 00:00:00.000000001

You can fix the problem by casting the dates to strings before groupby/agg, but if you try to cast the returned datetimes to ints, errors go off in some versions of pandas. Either way, aggregating by length should always return an int. Also, this may be similar to #11442, which was just posted.

michaelbilow reopened this Oct 27, 2015

Contributor

jreback commented Oct 27, 2015

this is a dupe of #10972

thanks for the report

jreback closed this Oct 27, 2015

jreback added this to the 0.20.0 milestone Feb 27, 2017

@jreback jreback added a commit to jreback/pandas that referenced this issue Feb 27, 2017

@jreback jreback BUG: fix groupby.aggregate resulting dtype coercion, xref #11444
make sure .size includes the name of the grouped
6a90819

@jreback jreback added a commit to jreback/pandas that referenced this issue Feb 27, 2017

@jreback jreback BUG: fix groupby.aggregate resulting dtype coercion, xref #11444, #13046


make sure .size includes the name of the grouped
11cb51f

@jreback jreback added a commit to jreback/pandas that referenced this issue Feb 27, 2017

@jreback jreback BUG: fix groupby.aggregate resulting dtype coercion, xref #11444, #13046


make sure .size includes the name of the grouped
61fa8be

@AnkurDedania AnkurDedania added a commit to AnkurDedania/pandas that referenced this issue Mar 21, 2017

@jreback @AnkurDedania jreback + AnkurDedania BUG: fix groupby.aggregate resulting dtype coercion, xref #11444, #13046


make sure .size includes the name of the grouped
d18d0b4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment