Stromael
Stromael

Reputation: 178

How do I remove the year and month from a DatetimeIndex in pandas?

I have a pandas dataframe:

import numpy as np
import pandas as pd
idx = pd.DataFrame(["2017-01-01 00:00:41","2017-01-01 00:06:53",\
                   "2017-01-01 00:07:10"],columns=["DateTime"])
df = pd.DataFrame([221,676,356],columns=["Value"])
df.index = pd.DatetimeIndex(idx["DateTime"])
df

which produces something like

      DateTime         Value
2017-01-01 00:13:41     221
2017-01-02 00:06:53     676
2017-01-05 00:22:10     356

What I would like to do next is remove the year and month information from this DateTime index, so as to produce

  DateTime     Value
01 00:13:41     221
02 00:06:53     676
05 00:22:10     356

I know that in the DataFrame idx I can drop it as follows:

idx["DateTime"] = idx["DateTime"].str(8:)

The problem is that this is no longer recognised by pandas as a DatetimeIndex. Moreover plotting this becomes very tricky (something I'd like to do subsequently). Any ideas how I can achieve this? (I'm sure it can be done, Python/pandas are too versatile not to have some cunning trick for achieving it!)

Upvotes: 2

Views: 7522

Answers (3)

iamklaus
iamklaus

Reputation: 3770

This works for me (updated)

df

                    Datetime  Values
0 2018-10-31 21:24:08.380554     258
1 2018-10-31 21:24:09.002616     586

df['Datetime'] = df['Datetime'].apply(lambda x: datetime.strftime(x, "%d %H:%M:%S"))

      Datetime  Values
0  31 21:24:08     258
1  31 21:24:09     586

Upvotes: 1

Stephen C
Stephen C

Reputation: 2036

If you just want the time, this would be what you are looking for

df.index = df.index.time

>>>df
          Value
00:00:41    221
00:06:53    676
00:07:10    356

>>>type(df.index[0])
datetime.time

As far as I can tell, the two possible objects are time objects and datetime objects, which means either way, you would need something for the year...

You could try to just standardize the year across them all like this:

df.index = [pd.datetime.strptime(s, '%d %HH-%MM-%SS') 
               for s in [pd.datetime.strftime(values, "%d %HH-%MM-%SS") 
               for values in df.index]]

(Just a first attempt... maybe a simpler way to accomplish this...)

Upvotes: 1

Mohamed Thasin ah
Mohamed Thasin ah

Reputation: 11192

try this,

print (df['DateTime'].dt.day).astype(str) +' '+ (df['DateTime'].dt.time).astype(str)

Output:

0    1 00:13:41
1    2 00:06:53
2    5 00:22:10
Name: DateTime, dtype: object

Upvotes: 0

Related Questions