Question: I have the data for 1 year which is time series data hourly basis so few timestamps are missing in between them.The shape of this data is (8188, 3) sample data I have attached below. I am resampling timestamps according to my data duration which will generate all the timestamps of one year even which were missing in my original data df_hourly = temp_df.resample('h').asfreq() the shape of resampled index is (8764, 1) Now, I am taking the difference of resampled data and original data new_rows = df_hourly.index.difference(original_index) so actual index shape should come as (8764-8188=576,) and then I will replace these 576 missing timestamp with median of total temp_df = temp_df[temp_df['cell'] == cell] print(temp_df.head()) temp_df.to_csv('temp_df.csv') print(temp_df.shape) # get_missing_duplicates(temp_df) # fill_missing_duplicates() print(temp_df.shape) temp_df['_time'] = pd.to_datetime(temp_df['_time']) print(temp_df['_time'].dtype) temp_df.set_index('_time', inplace=True) original_index = temp_df.index print(original_index) print("original_index",original_index.shape) df_hourly = temp_df.resample('h').asfreq() print(df_hourly) # This line is not working as expected new_rows = df_hourly.index.difference(original_index) print("&&&&&&&&&&&&",original_index) median_value = df['Total'].median() # new_rows = df_hourly.index.difference(temp_df.index) print("new_rows",new_rows)  new_rows = df_hourly.index.difference(original_index) this line gives the wrong result basically it should return the difference df_hourly.index and  the shape of temp_df is coming as (8188,) the shape of df_hourly is coming as (8764,) the shape of new_rows is also coming as (8764,)  The output of the temp_df.index is DatetimeIndex(['2023-05-22 02:00:04+00:00', '2023-05-22 03:00:03+00:00', '2023-05-22 04:00:03+00:00', '2023-05-22 05:00:03+00:00', '2023-05-22 06:00:03+00:00', '2023-05-22 07:00:03+00:00', '2023-05-22 08:00:03+00:00', '2023-05-22 09:00:03+00:00', '2023-05-22 10:00:03+00:00', '2023-05-22 11:00:03+00:00', ... '2024-05-20 17:00:03+00:00', '2024-05-20 18:00:04+00:00', '2024-05-20 20:00:03+00:00', '2024-05-20 21:00:03+00:00', '2024-05-20 22:00:03+00:00', '2024-05-20 23:00:03+00:00', '2024-05-21 01:00:03+00:00', '2024-05-21 02:00:03+00:00', '2024-05-21 04:00:03+00:00', '2024-05-21 05:00:03+00:00'], dtype='datetime64[ns, UTC]', name='_time', length=8188, freq=None)  The output of the df_hourly.index is: df_hourly_index RangeIndex(start=0, stop=8764, step=1) (8764, 3)  Sample data: 

Answer: pandas 2.2.1 Problem with resampling of non-uniform timestamps The issue is not with the code line you mentioned, but with your data. If you look at your time values, you will see that after a whole hour, a few seconds passed before a recording was made: '2023-05-22 02:00:04+00:00' - 4 seconds after 2:00 '2023-05-22 03:00:03+00:00' - 3 seconds after 3:00  Hourly resampling eliminates these sporadic biases. Therefore, you will end up with different indexes that may not overlap at all with the original timestamps. So you may need to round your time before resampling in order to find out missing timestamps, sort of: temp_df['_time'] = pd.to_datetime(temp_df['_time'], utc=True).dt.round('h')  As an alternative, you could find missing timestamps by counting data in each hour. In your code, it could look like this: new_rows = df_hourly.index[temp_df.resample('h').size() == 0]  If for some reason we need to keep the original timestamps, then I think that asfreq() may not be the best choice; using first() looks more reasonable. Also, I'd add in this case some negative offset when resampling, just in case if some recordings were made a few seconds before the end of an hour, something like this: df_hourly = temp_df.resample('h', offset=pd.Timedelta('-5min')).first()  If the reason to find the difference between the resampled timestamps and the original ones is to fill in the blanks, we can use fillna(...) instead. Code to experiment with The following sample data was taken from the original post with one change to represent a possible problem (see "a second before ..." in the code and a notion about the offset= parameter of resample): import pandas as pd timestamps = [ '2024-05-20 17:00:03+00:00', '2024-05-20 18:00:04+00:00', # missing '2024-05-20 20:00:03+00:00', '2024-05-20 20:59:59+00:00', # a second before 21:00:00 '2024-05-20 22:00:03+00:00', '2024-05-20 23:00:03+00:00', # missing '2024-05-21 01:00:03+00:00', '2024-05-21 02:00:03+00:00', # missing '2024-05-21 04:00:03+00:00', '2024-05-21 05:00:03+00:00' ] values = [403, 369, 375, 394, 398, 372, 335, 385, 415, 383] df = pd.DataFrame({ 'time': pd.to_datetime(timestamps, utc=True), 'value': values} )  Option 1: Keep the the original timestamps (no rounding) five_min = pd.Timedelta('5min') grouped_by_hour = df.set_index('time').resample('h', offset=-five_min) resampled_data = grouped_by_hour.first() is_new_record = grouped_by_hour.size() == 0 resampled_data.loc[is_new_record, 'value'] = round(df['value'].median()) resampled_data['value'] = resampled_data.astype({'value': int}) resampled_data.index += five_min print('Resampled_data'.upper(), resampled_data, '\nNew records'.upper(), resampled_data.index[is_new_record], sep='\n')  Option 2: Round the timestamps to the nearest hour before resampling rounded_original_time = df['time'].dt.round('h') resampled_data = ( df[['value']] .set_index(rounded_original_time) .resample('h') .asfreq() ) new_records = resampled_data.index.difference(rounded_original_time) resampled_data.loc[new_records, 'value'] = round(df['value'].median()) print('Resampled_data'.upper(), resampled_data, '\nNew records'.upper(), new_records, sep='\n')