Pandas pd.to_datetime only keep time do not date

XY XUE

I have a dataframe(Pandas), with a column representing dates, in the following format:

import pandas as pd
import tushare as ts

data = ts.get_tick_data('600030',date='2019-06-28',src='tt') [['time','price','change','volume','amount']]
print(data.head())

Specifically, I would like to use pd.to_datetime convert the 'time' column to datetime.

The code are:

  import pandas as pd
  import tushare as ts

  data = ts.get_tick_data('600030',date='2019-06-28',src='tt') [['time','price','change','volume','amount']]
  data.index=pd.to_datetime(data['time'])
  del data['time']
  print(data.head())

The results are as follow:

                           price  change  volume    amount
   time                                                
   2019-06-29 09:25:04  23.85   -0.07    6825  16279485
   2019-06-29 09:30:02  23.85    0.00    2736   6529832
   2019-06-29 09:30:05  23.85    0.00    3964   9459955
   2019-06-29 09:30:08  23.85    0.00     665   1585346
   2019-06-29 09:30:11  23.87    0.02     348    830136

I only want the time with the datetime style but do not the date. Like this:

          price  change  volume    amount
time                                     
09:25:04  23.85   -0.07    6825  16279485
09:30:02  23.85    0.00    2736   6529832
09:30:05  23.85    0.00    3964   9459955
09:30:08  23.85    0.00     665   1585346
09:30:11  23.87    0.02     348    830136

So I need the the help.

tawab_shakeel

try this

df = pd.DataFrame(data={'date':['2019-06-29 09:25:04','2019-06-29 09:30:02'],
                       'col2':[2,3]})

df['time'] = pd.to_datetime(df['date']).dt.time

if you want to make this as index then just do

df.set_index('time',inplace=True)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

bad date recognition with pandas to_datetime

using time zone in pandas to_datetime

Convert string date time to pandas datetime

pandas to_datetime formatting

How keep only date variable without time-field string

Pandas to_datetime with multiindex

Pandas: Join Date and Time into one datetime column

Regarding keeping the date information only after using pd.to_datetime

Getting today's date using pandas to_datetime version 0.23.4

pandas to_datetime does not accept '24' as time

Pandas: How to group by a datetime column, using only the time and discarding the date

Python Pandas check that string is only "Date" or only "Time" or "Datetime"

Do not show time but only date in my Matplotlib

specify the time zone for pandas to_datetime function

How do you convert a dataframe of datetime to time only? Basically removing the date element in Python Pandas

Converting datetime only to time in pandas

Pandas to_datetime - setting the date when only times (HH:MM) are input

Pandas dataframe to_datetime() is converting date incorrectly

pandas to_datetime formats date from same column in different formats

Pandas: Integer date not recognized with pd.to_datetime(x)

Apply pandas to_datetime function to excel date format

split datetime column into date and time columns in pandas

Pandas datetime - keep time only as dtype datetime

what does to_datetime() do in python pandas?

Pandas to_datetime with estabilished time interval

Adding date to pd.to_datetime

convert javascript Date.now to pandas to_datetime

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

How pandas only modify the date but not the time?