My questions is how to remove the time part from the string of an index. I am importing a csv with minute stock data and the code looks like:
import pandas as pd
df = pd.read_csv(f'/Volumes/Seagate Portable/S&P 500 List/AAPL.txt')
df.columns = ['Extra', 'Dates', 'Open', 'High', 'Low', 'Close', 'Volume']
df.Dates = pd.to_datetime(df.Dates)
df.set_index('Dates', inplace=True)
df = df.between_time('9:30', '16:00')
df.drop(['Extra', 'Open', 'High', 'Volume', 'Low'], axis=1, inplace=True)
df = df.groupby(pd.Grouper(freq='D')).agg({'Close':'last'})
df.index[0]
This modifies the dataframe to find the last close price of the trading day. The output to this is:
Timestamp('2020-01-02 00:00:00', freq='D')
So my issue is that I want to remove the time (00:00:00) from the string. I dont want to do something like:
` df.index = df.index.date)`
or
df['date'] = pd.to_datetime(df.index).dt.date
This is because then the output of df.index[0] will be: datetime.date(2020, 1, 2) which is not what I am looking for. So what I tried was
for df_dates in df.index:
string_df = f"{df_dates}"
split_string_df = string_df.split(' ')
df_dates = split_string_df[0]
df.index[df_dates] = df_dates
This gives me the error TypeError: Index does not support mutable operations
So what I am looking for a code that will remove the time portion of the string in a way that after I can index out df['2020-2-12'] rather than df[datetime.date(2020, 1, 2)] (I am not sure, put I am assuming this means to modify the display rather than the actual index, but I dont know.)