search
Search
Login
Unlock 100+ guides
menu
menu
web
search toc
close
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
What does this mean?
Why is this true?
Give me some examples!
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

Pandas DataFrame | asof method

schedule Aug 10, 2023
Last updated
local_offer
PythonPandas
Tags
mode_heat
Master the mathematics behind data science with 100+ top-tier guides
Start your free 7-days trial now!

Pandas DataFrame.asof(~) method returns the last row that contains no NaN.

Parameters

1. wherelink | date or array-like of dates

The values of the index you want to check until. Typically, this would be a list of dates. Check the examples below for clarification.

2. subset | string or array-like of string | optional

The label of the columns to consider when checking for NaN. By default, all columns are considered.

Return Value

If where is a scalar, then a Series is returned. Otherwise, a DataFrame is returned.

WARNING

The index of the source DataFrame must be sorted, otherwise an error is raised.

Examples

Basic usage

Consider the following DataFrame:

df = pd.DataFrame({"A":[3,4,pd.np.NaN,2], "B":[5,6,7,8]}, index=[10,20,30,40])
df
A B
10 3.0 5
20 4.0 6
30 NaN 7
40 2.0 8

Notice how the index of the DataFrame is sorted. This is a prerequisite for using this method.

To get the last row without NaN before index value 35 (inclusive):

df.asof(35)
A 4.0
B 6.0
Name: 35, dtype: float64

Here, we obtain the 2nd row (row with index 20). The 4th row, which has an index value of 40, was ignored since it exceeds the specified index value of 35.

Dates as index

Consider the following DataFrame with a DatetimeIndex:

my_index = pd.DatetimeIndex(["2020-12-24", "2020-12-25", "2020-12-26", "2020-12-27"])
df = pd.DataFrame({"A":[3,4,pd.np.NaN,2], "B":[5,6,7,8]}, index=my_index)
df
A B
2020-12-24 3.0 5
2020-12-25 4.0 6
2020-12-26 NaN 7
2020-12-27 2.0 8

Here, the index values are dates, which again, are sorted.

To get the last rows without NaN before two specific dates (inclusive):

my_where = pd.DatetimeIndex(["2020-12-24", "2020-12-26"])
df.asof(my_where)
A B
2020-12-24 3.0 5.0
2020-12-26 4.0 6.0

Note the following:

  • the row values of 2020-12-25 are returned for the second row. This is because the row values of 2020-12-26 contain a NaN, and so the next candidate is the row values of 2020-12-25, which do not contain any NaN.

  • the new index values of the returned DataFrame are the ones you've specified in the parameter.

robocat
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...
thumb_up
0
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!