import datetime
import pandas as pd
values = [("a", datetime.date(2015,1,1), 30.),                                                                                                                                   
          ("a", datetime.date(2015,1,2), 25.)]                                                                                                                                   
columns = ["id", "date", "amount"]                                                                                                              
df = pd.DataFrame(values, columns=columns)                                                                                                                                                         
df_single = df.set_index("date")
df_multi = df.set_index(["id", "date"]) 
print(df_multi.index)
print(df_single.index)

Problem description

I have a pandasDataFrame containing a datetime.date column. When I set a multilevel index, the date column is converted to a datetime.datetime object, which does not happen when setting a single-level index. I have posted the question on SO , and I understand the answer. Still, I think this is an unexpected behavior.

Expected Output

The expected output is:

print(df_multi.index) 
# MultiIndex(levels=[['a'], [2015-01-01, 2015-01-02]],
#         labels=[[0, 0], [0, 1]],
#         names=['id', 'date'])

Output of pd.show_versions()

In [22]: pd.show_versions() INSTALLED VERSIONS ------------------ commit: None python: 3.6.0.final.0 python-bits: 64 OS: Linux OS-release: 3.16.0-38-generic machine: x86_64 processor: x86_64 byteorder: little LC_ALL: en_US.UTF-8 LANG: en_US.UTF-8 LOCALE: en_US.UTF-8 pandas: 0.19.2 nose: 1.3.7 pip: 9.0.1 setuptools: 27.2.0 Cython: 0.25.2 numpy: 1.11.3 scipy: 0.18.1 statsmodels: 0.6.1 xarray: None IPython: 5.1.0 sphinx: 1.5.1 patsy: 0.4.1 dateutil: 2.6.0 pytz: 2016.10 blosc: None bottleneck: 1.2.0 tables: 3.3.0 numexpr: 2.6.1 matplotlib: 2.0.0 openpyxl: 2.4.1 xlrd: 1.0.0 xlwt: 1.2.0 xlsxwriter: 0.9.6 lxml: 3.7.2 bs4: 4.5.3 html5lib: None httplib2: None apiclient: None sqlalchemy: 1.1.5 pymysql: None psycopg2: 2.6.2 (dt dec pq3 ext lo64) jinja2: 2.9.4 boto: 2.45.0 pandas_datareader: None

Comment From: jreback

see some discussion https://github.com/pandas-dev/pandas/issues/8802

datetime.date are not first class objects and so are generally coerced

Comment From: ldocao

Thanks @jreback ! However, after reading #8802, I don't see comments about Multi-index vs single-Index behavior, am I right ? I'm fine with date being coerced, but I would expect that pandas will do so in single-index too. What do you think ?

Comment From: jreback

@ldocao not sure what you mean. You have to work pretty hard to actually make a datetime.date stick around. Can you show an example?

Comment From: ldocao

I posted an example in this issue: if you use set_index("date") (instead of set_index(["id", "date"])), then the date object is not coerced as datetime.

Sorry, maybe my post wasn't clear enough: my initial intent was to highlight the different coercion strategy of single vs multi index. Do you need more information from me ?

Comment From: jreback

@ldocao you started with an object dtype; it happens to be datetime.date objects, but that requires you to coerce it (e.g. with pd.to_datetime). I wouldn't recommend you use these objects at all and that is the issue. If you insist on using them you have to do a lot of work.