christianbauer1
christianbauer1

Reputation: 536

Python/Pandas: Save freq of a dataframe as a variable

I create a DataFrame and set its frequency to '15T':

import pandas as pd

d = {'T': [1, 2, 3, 4], 'H': [3, 4, 5, 6]}
df = pd.DataFrame(data=d, index=['10.09.2018  13:15:00','10.09.2018  13:30:00', 
                                 '10.09.2018  13:45:00', '10.09.2018  14:00:00'])
df.index = pd.to_datetime(df.index)
df.index.freq = '15T'

I call the function df.index to see if everything worked fine.

df.index
Out[19]: 
DatetimeIndex(['2018-10-09 13:15:00', '2018-10-09 13:30:00',
               '2018-10-09 13:45:00', '2018-10-09 14:00:00'],
              dtype='datetime64[ns]', freq='15T')

My question: How can I call the freq in that index and save it to a variable? I tried the following but it didn't work.

a = df.index.freq

Could you give me some additional information in general on how to call elements of my Output in general ? E.g. if I wanted to save the dtype and not the frequency.

Upvotes: 1

Views: 235

Answers (1)

jezrael
jezrael

Reputation: 863501

Use DatetimeIndex.freqstr:

a = df.index.freqstr
print (a)
15T

Upvotes: 1

Related Questions