Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Frequency of a data frame

Tags:

python

pandas

I have a data frame indexed with a date (Python datetime object). How could I find the frequency as the number of months of data in the data frame?

I tried the attribute data_frame.index.freq, but it returns a none value. I also tried asfreq function using data_frame.asfreq('M',how={'start','end'} but it does not return the expected results. Please advise how I can get the expected results.

like image 232
karthikbharadwaj Avatar asked Apr 11 '14 04:04

karthikbharadwaj


People also ask

How do you find the frequency of a data frame?

In pandas you can get the count of the frequency of a value that occurs in a DataFrame column by using Series. value_counts() method, alternatively, If you have a SQL background you can also get using groupby() and count() method.

How do I find the frequency of a DataFrame in R?

Get Frequency of All Values into DataFrame In case you wanted to get the frequency count of values in a vector as an R dataframe use as. data. frame() function. This takes the result of table() function as input and returns a dataframe.

How do you count the frequency of a DataFrame in Python?

Using the count(), size() method, Series. value_counts(), and pandas. Index. value_counts() method we can count the number of frequency of itemsets in the given DataFrame.

How do you find the frequency of a column in R?

The sapply() method, which is used to compute the frequency of the occurrences of a variable within each column of the data frame. The sapply() method is used to apply functions over vectors or lists, and return outputs based on these computations.


2 Answers

You want to convert you index of datetimes to a DatetimeIndex, the easiest way is to use to_datetime:

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

Now you can do timeseries/frame operations, like resample or TimeGrouper.

If your data has a consistent frequency, then this will be df.index.freq, if it doesn't (e.g. if some days are missing) then df.index.freq will be None.

like image 160
Andy Hayden Avatar answered Oct 06 '22 00:10

Andy Hayden


You probably want to be use pandas Timestamp for your index instead of datetime to use 'freq'. See example below

import pandas as pd
dates = pd.date_range('2012-1-1','2012-2-1')
df = pd.DataFrame(index=dates)
print (df.index.freq)

yields,

<Day>

You can easily convert your dataframe like so,

df.index = [pd.Timestamp(d) for d in df.index]
like image 21
nitin Avatar answered Oct 05 '22 22:10

nitin