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

Problem with TickTime and TickDate in Python 3.10 #38

Open
gusostow opened this issue Aug 19, 2022 · 7 comments · May be fixed by #39
Open

Problem with TickTime and TickDate in Python 3.10 #38

gusostow opened this issue Aug 19, 2022 · 7 comments · May be fixed by #39
Labels

Comments

@gusostow
Copy link

[ins] In [26]: import fixfmt.npfmt
          ...: import numpy as np
          ...: ds = np.array([np.datetime64("2021-01-01")]*2, dtype="datetime64[ns]")
          ...: fixfmt.npfmt.choose_formatter_datetime64(ds)(ds[0])
          ...:
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-26-92a09492af6f> in <module>
      2 import numpy as np
      3 ds = np.array([np.datetime64("2021-01-01")]*2, dtype="datetime64[ns]")
----> 4 fixfmt.npfmt.choose_formatter_datetime64(ds)(ds[0])

TypeError: 'numpy.datetime64' object cannot be interpreted as an integer

Mentioned in the 3.10 changelog

Builtin and extension functions that take integer arguments no longer accept Decimals, Fractions and other objects that can be converted to integers only with a loss (e.g. that have the int() method but do not have the index() method). (Contributed by Serhiy Storchaka in bpo-37999.)

Python versions 3.8/9 warn

<string>:1: DeprecationWarning: an integer is required (got type numpy.datetime64).  Implicit conversion to integers using __int__ is deprecated, and may be removed in a future version of Python.
@alexhsamuel
Copy link
Owner

I see something slightly different:

>>> a = np.array(["2022-08-19"] * 2, "datetime64[ns]")
>>> f = fixfmt.npfmt.choose_formatter_datetime64(ds)
>>> f
TickTime(1000000000, -1, "NaT                      ")

This with head of master, and

>>> np.__version__
'1.23.2'

Which version of NumPy and fixfmt are you on?

However,

>>> f(a[0])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'numpy.datetime64' object cannot be interpreted as an integer

@alexhsamuel
Copy link
Owner

Hmm, I can't even find an entry in the 3.10 CAPI to call __int__() or the nb_int slot. :(

np.datetime64 provides __int__ but not __index__, so I'm not sure what's best to do here.

@gusostow
Copy link
Author

gusostow commented Aug 19, 2022

I'm on

fixfmt                    0.13.2              
numpy                     1.23.2         

but my example is consistent with yours, albeit with an overloaded line, because I'm calling the formatter on the datetime64 value to raise the exception:

fixfmt.npfmt.choose_formatter_datetime64(ds)(ds[0])

Hmm, I can't even find an entry in the 3.10 CAPI to call __int__() or the nb_int slot. :(

np.datetime64 provides __int__ but not __index__, so I'm not sure what's best to do here.

Is it possible to explicitly cast as an int before whatever operation is raising this?

@alexhsamuel
Copy link
Owner

alexhsamuel commented Aug 19, 2022

Is it possible to explicitly cast as an int before whatever operation is raising this?

For whom? You (the caller) can certainly do this. That's more or less the intention of the TickTime and TickDate formatters, which were always intended to be a stop-gap until I had time to make fixfmt use ora for time/date formatting.

What I was saying, though, is that I don't see a way to do this through the CAPI. All PyLong_* methods now use __index__ instead of __int__. I think I can call through tp_as_number->nb_int directly, if it's set; this is hacky but should work for this case.

Begs the question what nb_int is used for anymore. Maybe just the Python-visible int() function? Seems weird that there is no CAPI equivalent.

@alexhsamuel
Copy link
Owner

alexhsamuel commented Aug 19, 2022

Oh: PyNumber_Long().

@alexhsamuel
Copy link
Owner

alexhsamuel commented Aug 19, 2022

Arg!

>>> int(np.datetime64('2022-08-19T12:34:56.789'))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: int() argument must be a string, a bytes-like object or a real number, not 'datetime.datetime'

@alexhsamuel alexhsamuel changed the title Problem with TickTime in Python 3.10 Problem with TickTime and TickDate in Python 3.10 Aug 19, 2022
@alexhsamuel alexhsamuel linked a pull request Sep 2, 2022 that will close this issue
@alexhsamuel
Copy link
Owner

Could you please check if #39 and ora 0.7.1 work for you? Please check also that you are not using TickTime or TickDate or add_tick_time_column directly?

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

Successfully merging a pull request may close this issue.

2 participants