stefan
stefan

Reputation: 61

Make a date less specific in pandas

I have a csv file with dates inside as index.

I use the command below to read the csv file :

pd_date=pd.read_csv("path/file.csv",index_col="created_at")

The output of this command is :

                    user_screen_name
created_at                                           
2019-02-22 03:27:07      ...   
2019-02-21 23:10:38      ...  
2019-02-21 19:09:57      ... 
2019-02-21 17:17:45      ... 

As you can see, the "format" is :

2019-02-22 03:27:07           year-month-day hours:minutes:seconds

I would like to find a command to remove the hours, minutes and seconds.

The result I would like to reach is :

            user_screen_name
created_at                                           
2019-02-22       ...   
2019-02-21       ...  
2019-02-21       ... 
2019-02-21       ... 

Thank you by advance for your help.

Upvotes: 0

Views: 76

Answers (2)

Chris
Chris

Reputation: 29742

Given df:

                     Value
Date                      
2019-02-22 03:27:07      1
2019-02-21 23:10:38      2
2019-02-21 19:09:57      3
2019-02-21 17:17:45      4

If index is datetime

df.index = df.index.date

If it is just str:

df.index = pd.to_datetime(df.index).date

And both yield:

print(df)
            Value
2019-02-22      1
2019-02-21      2
2019-02-21      3
2019-02-21      4

Upvotes: 0

JimmyA
JimmyA

Reputation: 686

You can use pandas datetime date function to return just the date:

str(pd.to_datetime('now').date())

returns:

'2019-02-22'

Upvotes: 1

Related Questions