Skip to content Skip to sidebar Skip to footer

Pandas: Calling Df.loc[] From An Index Consisting Of Pd.datetime

Say I have a df as follows: a=pd.DataFrame([[1,3]]*3,columns=['a','b'],index=['5/4/2017','5/6/2017','5/8/2017']) a.index=pd.to_datetime(a.index,format='%m/%d/%Y') The type of

Solution 1:

Here's my best shot:

Pandas has an internal function called pandas._guess_datetime_format. This is what gets called when passing the 'infer_datetime_format' argument to pandas.to_datetime. It takes a string and runs through a list of "guess" formats and returns its best guess on how to convert that string to a datetime object.

Referencing a datetime index with a string may use a similar approach.

I did some testing to see what would happen in the case you described - where a dataframe contains both the date 2017-04-05 and 2017-05-04.

In this case, the following:

df.loc['5/4/2017']

Returned the Data for May 4th, 2017

df.loc['4/5/2017']

Returned the data for April 5th, 2017.

Attempting to reference 4/5/2017 in your original matrix gave an "is not in the [index]" error.

Based on this, my conclusion is that pandas._guess_datetime_format defaults to a "%m/%d/%Y" format in cases where it cannot be distinguished from "%d/%m/%Y". This is the standard date format in the US.

Post a Comment for "Pandas: Calling Df.loc[] From An Index Consisting Of Pd.datetime"