0

I have a dataframe in 1 column with all different times.

Time
-----
10:00
11:30
12:30
14:10
...

I need to do a quantile range on this dataframe with the code below:

df.quantile([0,0.5,1],numeric_only=False)

Following the link below, the quantile does work. https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.quantile.html

As my column as in object, I need to convert to pd.datetime or pd.Timestamp. When I convert to pd.datetime, I will have all my time inserted with dates too. If I format it to %H:%M, the column turns back to object which cannot work with quantile under numeric_only mode.

How can I convert to datetime format in %H:%M and still stick to datetime format?

Below was the code I used:

df = pd.DataFrame({"Time":["10:10","09:10","12:00","13:23","15:23","17:00","17:30"]})
df['Time2'] = pd.to_datetime(df['Time']).dt.strftime('%H:%M')
df['Time2'] = df['Time2'].astype('datetime64[ns]')
user3782604
  • 330
  • 1
  • 19

1 Answers1

1

How can I convert to datetime format in %H:%M and still stick to datetime format?

Impossible in pandas, maybe closer is use timedeltas:

df = pd.DataFrame({"Time":["10:10","09:10","12:00","13:23","15:23","17:00","17:30"]})
df['Time2'] = pd.to_timedelta(df['Time'].add(':00'))
print (df)
    Time    Time2
0  10:10 10:10:00
1  09:10 09:10:00
2  12:00 12:00:00
3  13:23 13:23:00
4  15:23 15:23:00
5  17:00 17:00:00
6  17:30 17:30:00
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252