Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

python pandas | replacing the date and time string with only time

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')

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading