Adding Missing Dates In Datetime Index In Pandas DataFrame

menu home About store Store paid Pricing login Log in team_dashboard Dashboard near_me Linear Algebra casino Prob and Stats function Other math topics smart_toy Machine Learning Pandas NumPy Matplotlib PySpark MySQL Dagster (NEW) SkyTowner BETA search Search Login Unlock 100+ guides menu menu web SkyTowner search toc close history_edu Blog help Help Outline ExampleSolution 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. fullscreen_exit Shrink north_east Navigate to Pandas 655 guides keyboard_arrow_down near_me Linear Algebra 54 guides casino Prob and Stats 38 guides smart_toy Machine Learning 36 guides function Other math topics 10 guides Dagster 11 guides Pandas 655 guides NumPy 319 guides Matplotlib 83 guides PySpark 147 guides MySQL 295 guides chevron_leftHandling Missing ValuesAdding missing dates in Datetime IndexChecking if a certain value in a DataFrame is NaNChecking if a DataFrame contains any missing valuesConverting a column with missing values to integer typeCounting non-missing valuesCounting number of rows with missing valuesCounting the number of NaN in each row of a DataFrameCounting number of NaN values in each column of a DataFrameCounting the total number of NaN values of a DataFrameFilling missing values using another columnFilling missing values with the mean of the columnFinding columns with missing valuesGetting integer indexes of rows with NaNGetting rows with missing valuesGetting rows with missing values in certain columnsGetting index of rows with missing values (NaNs)Getting index of rows without missing valuesMapping NaN values to 0 and non-NaN values to 1Mapping NaN values to False and non-NaN values to TrueRemoving columns where some rows contain missing valuesRemoving rows from a DataFrame with missing valuesReplacing all NaN values of a DataFrameReplacing all NaN values with zeros in a DataFrameReplacing missing valuesReplacing missing values with constantsReplacing NaN with blank stringReplacing NaNs for certain columnsReplacing NaNs with preceding valuesReplacing values with NaNsUsing interpolation to fill missing values check_circle Mark as learned thumb_up 5 thumb_down 2 chat_bubble_outline 0 Comment auto_stories Bi-column layout settings Adding missing dates in Datetime Index in Pandas DataFrame schedule Aug 11, 2023 Last updated local_offer PythonPandas Tags tocTable of Contents expand_more ExampleSolution mode_heat Master the mathematics behind data science with 100+ top-tier guides Start your free 7-days trial now! Example

Consider the following DataFrame:

index_date = pd.date_range(start="2020-12-20", end="2020-12-23", freq="2D")df = pd.DataFrame({"A":[2,3],"B":[4,5]}, index=index_date)df A B2020-12-20 2 42020-12-22 3 5

Notice how we have a missing date in the index of df.

Solution

We can add the missing date in the DatetimeIndex by replacing it with a new index using reindex(~):

new_date_range = pd.date_range(start="2020-12-20", end="2020-12-22", freq="D")df.reindex(new_date_range, fill_value=0) A B2020-12-20 2 42020-12-21 0 02020-12-22 3 5 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... Outline ExampleSolution thumb_up 5 thumb_down 2 chat_bubble_outline 0 settings Enjoy our search Hit / to insta-search docs and recipes! Navigation Home Blog Contact us Resources Python Pandas MySQL Beautiful Soup Matplotlib NumPy PySpark Community Join our Discord mail Join our newsletter for updates on new comprehensive DS/ML guides Terms | Privacy

Tag » Add Datetimeindex To Pandas Dataframe