0

I use python and pandas to get yahoo stock market date and save to a local csv file.

DailyPrice = pd.read_csv(file, index_col=0)
print(DailyPrice.tail())

Date
2020-05-27           86.480003  84.370003  86.300003  86.180000  1917600.0 
2020-05-28           87.849998  86.059998  86.870003  86.690002  1908700.0

then I update the date.

History = pdr.DataReader(ticker, start=Before_yesterday, end=Today, data_source='yahoo')
print(History.tail())

Date
2020-06-29  87.360001  86.110001  86.559998  87.290001  1302500.0  87.290001
2020-06-30  88.379997  87.235001  87.330002  88.269997   236377.0  88.269997

then I append date

DailyPrice = DailyPrice.append(History)
print(DailyPrice.tail())

Date
2020-05-27           86.480003  84.370003  86.300003  86.180000  1917600.0 
2020-05-28           87.849998  86.059998  86.870003  86.690002  1908700.0
2020-06-29 00:00:00  87.360001  86.110001  86.559998  87.290001  1302500.0
2020-06-30 00:00:00  88.379997  87.235001  87.330002  88.269997   236377.0

so, my question is : how coul I format the column Date just like %Y-%m-%d, without time hh:mm:ss?

wenny0501
  • 1
  • 2
  • from the example data you posted, it looks like you have mixed data types or only string data type in `df['Date']` - you might want to convert to datetime first; `df['Date'] = pd.to_datetime(df['Date'], errors='coerce')`. After that you should be able to format via `df['Date'].dt.date`. – FObersteiner Jun 30 '20 at 16:43
  • Does this answer your question? [Keep only date part when using pandas.to\_datetime](https://stackoverflow.com/questions/16176996/keep-only-date-part-when-using-pandas-to-datetime) – ggorlen Jan 29 '23 at 05:35

3 Answers3

0

I guess it is the same question as this one

df['Date'] = df['Date'].dt.date

It should work

xzelda
  • 172
  • 7
0

Looks like you have a DatetimeIndex, you could try to use strftime

DailyPrice.index = DailyPrice.index.strftime('%Y-%m-%d')
Kikuchiyo
  • 3
  • 1
  • 2
0

I think I find the problem.

Date is not a name of column.

DailyPrice.columns.values.tolist()
['High', 'Low', 'Open', 'Close', 'Volume', 'Adj Close']

so, the first column, Date, what's it?

Date is the index.

at last, issue is resolved.

DailyPrice.index = pd.to_datetime(DailyPrice.index, format = '%Y-%m-%d')
wenny0501
  • 1
  • 2