Skip to content

groupby.idxmin() returns datetime values for datatime columns #25444

Closed
@tmitanitky

Description

@tmitanitky

Code Sample, a copy-pastable example if possible

import pandas as pd

df = pd.DataFrame({'name': ['A', 'A', 'A', 'B', 'B'], 'a':[1,2,3,4,5], 'date': ['2016', '2017', '2018', '2016', '2017']}, )
df['date'] = pd.to_datetime(df['date'])

#current behaviour
df.groupby('name').idxmin()
>>       a                          date
>> name                                 
>> A     0 1970-01-01 00:00:00.000000000
>> B     3 1970-01-01 00:00:00.000000003

Problem description

The returning df's dtypes would be the same as that of index.
Thanks.

Expected Output

# expected behaviour
df.groupby('name').idxmin().astype('int')
>>       a  date
>> name         
>> A     0     0
>> B     3     3

Output of pd.show_versions()

[paste the output of pd.show_versions() here below this line]
INSTALLED VERSIONS

commit: None
python: 3.6.6.final.0
python-bits: 64
OS: Darwin
OS-release: 18.0.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: None
LANG: ja_JP.UTF-8
LOCALE: ja_JP.UTF-8

pandas: 0.23.4
pytest: 4.0.1
pip: 18.1
setuptools: 40.6.2
Cython: 0.29
numpy: 1.15.4
scipy: 1.1.0
pyarrow: None
xarray: None
IPython: 7.2.0
sphinx: 1.8.2
patsy: 0.5.1
dateutil: 2.7.5
pytz: 2018.7
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 3.0.2
openpyxl: 2.5.11
xlrd: 1.1.0
xlwt: 1.2.0
xlsxwriter: 1.1.2
lxml: 4.2.5
bs4: 4.6.3
html5lib: 1.0.1
sqlalchemy: 1.2.14
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions