Pandas datetime from mongodb

How do I query for a date in a mongodb document that has been opened in pandas?

Could someone refer me to an concise resource on handling datetime values from mongodb in python and pandas? There are confusing issues here that I am slowly figuring out.

WhatI have learned:

  •  Mongodb dates are milliseconds since epoch, a 64-bit integer value.
    
  •  Python datetime is seconds since epoch, a float value.
         in python datetime, milliseconds appear after a decimal point
    
  • convert mongodb date to python date:  mongodbDate / 1000 = pythonDate
    
  • timezones are handled in the application layer (which means me)
    

So when a mongodb document is read into a pandas dataframe, how is a date queried?
For example, select (iloc) data with a date of ‘2023-03-01’?

Apologies for what is no doubt a dumb question. The diversity of standards in managing datetimes creates challenges that are difficult to reason through.

1 Like

Found the problem - it’s iloc
iloc is purely positional.
for a logical mask, use loc
for example:

startDate = pd.Timestamp('2023-03-07')
end__Date = pd.Timestamp('2023-03-09')

mask = ((df['timestamp'] > startDate) & (df['timestamp'] <= end__Date))
df_masked = df.loc[mask]

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.