Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error dividing Period by timedelta #382

Open
emlyn opened this issue Jun 18, 2019 · 3 comments
Open

Error dividing Period by timedelta #382

emlyn opened this issue Jun 18, 2019 · 3 comments

Comments

@emlyn
Copy link

emlyn commented Jun 18, 2019

The following snippet fails for me (using Python 3.7.3).
It looks like pendulum is depending on a private method of timedelta (_to_microseconds), which does not exist in all versions:

> from datetime import timedelta
> from pendulum import datetime
> p = datetime(2019, 1, 1) - datetime(2019, 1, 1)
> d = timedelta(days=1)
> p/d
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-33-79e6ca0b60d8> in <module>
----> 1 p/d

~/.virtualenvs/test/lib/python3.7/site-packages/pendulum/period.py in __truediv__(self, other)
    325
    326     def __truediv__(self, other):
--> 327         return self.as_interval().__truediv__(other)
    328
    329     __div__ = __floordiv__

~/.virtualenvs/test/lib/python3.7/site-packages/pendulum/duration.py in __truediv__(self, other)
    357         usec = self._to_microseconds()
    358         if isinstance(other, timedelta):
--> 359             return usec / other._to_microseconds()
    360
    361         # Removing years/months approximation

AttributeError: 'datetime.timedelta' object has no attribute '_to_microseconds'
@The-Fonz
Copy link

I'm having this exact issue on Python 3.8.5 and Pendulum 2.1.2.

@jacob-r-g
Copy link

I am having this same issue

@AlexisBRENON
Copy link

AlexisBRENON commented Jul 2, 2024

Still facing this issue with Pendulum 3.0.0 and Python 3.11.
My current workaround is to manually convert division terms to seconds (I don't mind the microsecond parts).

Python 3.11.9 (main, May  2 2024, 11:51:32) [GCC 13.2.1 20240417] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pendulum
>>> pendulum.__version__
'3.0.0'
>>> p = pendulum.Duration(hours=10)
>>> import datetime
>>> p2 = datetime.timedelta(hours=1)
>>> p / p2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/alexis/Public/GitHub/<redacted>/pendulum/duration.py", line 419, in __truediv__
    float, usec / other._to_microseconds()  # type: ignore[attr-defined]
                  ^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'datetime.timedelta' object has no attribute '_to_microseconds'. Did you mean: 'microseconds'?
>>> p.as_timedelta() / p2
10.0
>>> p.total_seconds() / p2.total_seconds()
10.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants