Home > Enterprise >  python pandas | replacing the date and time string with only time
python pandas | replacing the date and time string with only time

Time:11-21

price quantity high time
10.4 3 2021-11-08 14:26:00-05:00
dataframe = ddg

the datatype for hightime is datetime64[ns, America/New_York]

i want the high time to be only 14:26:00 (getting rid of 2021-11-08 and -05:00) but i got an error when using the code below

ddg['high_time'] = ddg['high_time'].dt.strftime('%H:%M')

CodePudding user response:

I think because it's not the right column name:

# Your code
>>> ddg['high_time'].dt.strftime('%H:%M')
...
KeyError: 'high_time'


# With right column name
>>> ddg['high time'].dt.strftime('%H:%M')
0    14:26
Name: high time, dtype: object


# My dataframe:
>>> df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 1 entries, 0 to 0
Data columns (total 3 columns):
 #   Column     Non-Null Count  Dtype                           
---  ------     --------------  -----                           
 0   price      1 non-null      float64                         
 1   quantity   1 non-null      int64                           
 2   high time  1 non-null      datetime64[ns, America/New_York]
dtypes: datetime64[ns, America/New_York](1), float64(1), int64(1)
memory usage: 152.0 bytes
  • Related