search
Search
Publish
menu
menu search toc more_vert
Robocat
Guest 0reps
Thanks for the thanks!
close
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
help Ask a question
Share on Twitter
search
keyboard_voice
close
Searching Tips
Search for a recipe: "Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview
icon_star
Doc Search
icon_star
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Navigate to
A
A
share
thumb_up_alt
bookmark
arrow_backShare
Twitter
Facebook

Pandas DataFrame | asfreq method

Programming
chevron_right
Python
chevron_right
Pandas
chevron_right
Documentation
chevron_right
DataFrame
chevron_right
Time Series
schedule Mar 9, 2022
Last updated
local_offer PythonPandas
Tags

Pandas DataFrame.asfreq(~) method returns a DataFrame with a new time index that respects the specified frequency.

Parameters

1. freqlink | DateOffset or string

The frequency used to create a new time index.

2. methodlink | None or string | optional

The method by which to fill the resulting NaNs:

Value

Description

Fill with the next valid observation.

"bfill"

or

"backfill"

Fill with the previous valid observation.

"ffill" or "pad"

None

Leave NaNs intact.

By default, method=None.

3. normalizelink | boolean | optional

Whether or not to set the time of day to midnight. By default, normalize=False.

4. fill_valuelink | scalar | optional

The value to fill missing values with. Note that missing values that existed in the source DataFrame will not be filled - only those that arise due to this method will be filled. By default, fill_value=None.

Return Value

A DataFrame with a new time index that respects the specified frequency.

Examples

Consider the following DataFrame:

index_date = pd.date_range("2020-12-25", periods=3, freq="T")
df = pd.DataFrame({"A":[1,2,3],"B":[4,5,6]}, index=index_date)
df
                     A  B
2020-12-25 00:00:00  1  4
2020-12-25 00:01:00  2  5
2020-12-25 00:02:00  3  6

Basic usage

To set a new time index with a frequency of 30 seconds:

df.asfreq(freq="30S")
                     A    B
2020-12-25 00:00:00  1.0  4.0
2020-12-25 00:00:30  NaN  NaN
2020-12-25 00:01:00  2.0  5.0
2020-12-25 00:01:30  NaN  NaN
2020-12-25 00:02:00  3.0  6.0

The reason why we get NaN for some rows is that their index is not present in the source DataFrame df. For instance, we get NaN for the second row because df does not contain a row with index 2020-12-25 00:00:30.

Filling methods

None

By default, method=None, which means that new values will be marked as NaN and no filling rules will be applied:

df.asfreq(freq="30S")   # method=None
                     A    B
2020-12-25 00:00:00  1.0  4.0
2020-12-25 00:00:30  NaN  NaN
2020-12-25 00:01:00  2.0  5.0
2020-12-25 00:01:30  NaN  NaN
2020-12-25 00:02:00  3.0  6.0

ffill

To fill using the previous valid observation:

df.asfreq(freq="30S", method="ffill")
                     A  B
2020-12-25 00:00:00  1  4
2020-12-25 00:00:30  1  4
2020-12-25 00:01:00  2  5
2020-12-25 00:01:30  2  5
2020-12-25 00:02:00  3  6

bfill

To fill using the next valid observation:

df.asfreq(freq="30S", method="bfill")
                     A  B
2020-12-25 00:00:00  1  4
2020-12-25 00:00:30  2  5
2020-12-25 00:01:00  2  5
2020-12-25 00:01:30  3  6
2020-12-25 00:02:00  3  6

Normalising the index

Consider the following DataFrame:

index_date = pd.date_range("2020-12-25", periods=3, freq="T")
df = pd.DataFrame({"A":[1,2,3],"B":[4,5,6]}, index=index_date)
df
A B
2020-12-25 00:00:00 1 4
2020-12-25 00:01:00 2 5
2020-12-25 00:02:00 3 6

To reset the time of day to midnight, set normalize=True:

df.asfreq(freq="MIN", normalize=True)
A B
2020-12-25 1 4
2020-12-25 2 5
2020-12-25 3 6

Specifying a fill value

Consider the same df as above:

df
A B
2020-12-25 00:00:00 1 4
2020-12-25 00:01:00 2 5
2020-12-25 00:02:00 3 6

Instead of the default NaNs, we can choose the value to fill with using fill_value:

df.asfreq(freq="30S", fill_value=0)
A B
2020-12-25 00:00:00 1 4
2020-12-25 00:00:30 0 0
2020-12-25 00:01:00 2 5
2020-12-25 00:01:30 0 0
2020-12-25 00:02:00 3 6

Case when NaNs already exist in DataFrame

Consider the following DataFrame:

index_date = pd.date_range("2020-12-25", periods=3, freq="T")
df = pd.DataFrame({"A":[1,2,3],"B":[4,5,pd.np.NaN]}, index=index_date)
df
                     A  B
2020-12-25 00:00:00  1  4.0
2020-12-25 00:01:00  2  5.0
2020-12-25 00:02:00  3  NaN

Here, our df has a NaN.

Calling the asfreq(~) method:

df.asfreq(freq="30S", fill_value=8)
                     A  B
2020-12-25 00:00:00  1  4.0
2020-12-25 00:00:30  8  8.0
2020-12-25 00:01:00  2  5.0
2020-12-25 00:01:30  8  8.0
2020-12-25 00:02:00  3  NaN

Notice how the original NaN is not filled. - only NaN that arise due to the method will be filled.

robocat
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Ask a question or leave a feedback...