0

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

1 Answer 1

1

Did you try:

indexes =[]
for df_dates in df.index:
    string_df = f"{df_dates}"
    split_string_df = string_df.split(' ')
    df_dates = split_string_df[0]
    indexes.append( df_dates)
df.index = indexes

The problem is you tried to modify the indexes by index by calling df.index[<something>] = <otherthing> because the index list is immutable and have to be modified at whole..

Sign up to request clarification or add additional context in comments.

3 Comments

This worked thank you! I didn't think of that, I appreciate your help
With pleasure ...!
Yes sorry I was going to right when you put up the answer, I just had to wait the minute because it wont let you accept the answer right away, and I forgot to come back. My apologies

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.