Problems with deducting dates in datetime format from one another

Summary

Buggy handling of datetime dtypes on Streamlit Cloud. It runs ok locally.

Steps to reproduce

Code snippet:

video_df['publishedAt'] = pd.to_datetime(video_df['publishedAt'])
video_df['publishedDate'] = video_df['publishedAt'].dt.date
today = datetime.today().date()
video_df['today'] = pd.to_datetime(today).date()
video_df['days_since_published'] = (video_df['today'] - video_df['publishedDate']).dt.days

Last line is causing the following error:

raise AttributeError(“Can only use .dt accessor with datetimelike values”)

If applicable, please provide the steps we should take to reproduce the error or specified behavior.

Expected behavior:

Both columns (video_df[‘today’] and video_df[‘publishedDate’]) have been converted to datetime dates and the difference between them should return a number of days

Actual behavior:
I get an error:
E ```
Traceback (most recent call last):


File “/home/appuser/venv/lib/python3.9/site-packages/streamlit/runtime/scriptrunner/script_runner.py”, line 565, in _run_script


exec(code, module.__dict__)

File “/app/youtube_channels_by_topic_streamlit/YouTube_channels_by_topic_streamlit.py”, line 369, in


video_df['days_since_published'] = (video_df['today'] - video_df['publishedDate']).dt.days

File “/home/appuser/venv/lib/python3.9/site-packages/pandas/core/generic.py”, line 5989, in getattr


return object.__getattribute__(self, name)

File “/home/appuser/venv/lib/python3.9/site-packages/pandas/core/accessor.py”, line 224, in get


accessor_obj = self._accessor(obj)

File “/home/appuser/venv/lib/python3.9/site-packages/pandas/core/indexes/accessors.py”, line 580, in new


raise AttributeError("Can only use .dt accessor with datetimelike values")

AttributeError: Can only use .dt accessor with datetimelike values

I also have a feeling that there might be some cached variable values that do not refresh after I update the code. Streamlit cloud runs the new code after pushing to Github immediately but I feel like it is the old code from before. 

### Debug info

- Streamlit version: Streamlit, version 1.22.0
- Python version: (get it with `$ python --version`)
- Using Conda? venv
- OS version: macOS 13.3.1 (a) (22E772610a)
- Browser version: Brave Version 1.51.114 Chromium: 113.0.5672.92 (Official Build) (arm64)

### Requirements file

streamlit

pandas

plotly

requests

tweepy

isodate

datetime

google-api-python-client

matplotlib

seaborn

wordcloud

### Links

* Link to your GitHub repo:
* Link to your deployed app:

### Additional information

There has been some other tickets that mentioned problems with timedelta datatype and it might be related.

The difference between two date objects is not another date object, but a timedelta object. Check datetime — Basic date and time types — Python 3.12.0 documentation, I think you’re looking for the total_seconds() method.

1 Like

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