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

NDarray of DateTime #119

Open
henon opened this issue Mar 29, 2023 · 3 comments
Open

NDarray of DateTime #119

henon opened this issue Mar 29, 2023 · 3 comments

Comments

@henon
Copy link
Contributor

henon commented Mar 29, 2023

          @henon I wouldn't suppose any workarounds exist to getting something like this to work?
NDarray<DateTime> dateTimes = np.array( new DateTime[] {
    new DateTime(2000, 1, 15),
    new DateTime(2000, 6, 15),
    new DateTime(2001, 1, 15),
    new DateTime(2001, 6, 15)
});

Originally posted by @amine-aboufirass in #50 (comment)

@henon
Copy link
Contributor Author

henon commented Mar 29, 2023

I don't know. Can you post an equivalent snippet in Python?

@amine-aboufirass
Copy link

amine-aboufirass commented Mar 29, 2023

@henon Well there's multiple ways. I think this is the most idiomatic:

import numpy as np

datetimes = np.array(
    [
        '2000-01-15', 
        '2000-06-15', 
        '2001-01-15',
        '2001-06-15'
    ], 
    dtype='datetime64'
)

print(datetimes.dtype)
print(np.diff(datetimes))

You can also do this:

import numpy as np
from datetime import datetime

datetimes = [
    np.datetime64('2000-01-15'),
    np.datetime64('2000-06-15'),
    np.datetime64('2001-01-15'),
    np.datetime64('2001-06-15')
]

datetimes = np.array(datetimes)

print(np.diff(datetimes).dtype)
print(np.diff(datetimes))

But if I had to emulate the C# as closely as I could, I guess I was attempting something like this:

import numpy as np
from datetime import datetime

datetimes = [
    datetime(2000, 1, 15),
    datetime(2000, 6, 15),
    datetime(2001, 1, 15),
    datetime(2001, 6, 15)
]

datetimes = np.array(datetimes)

print(np.diff(datetimes).dtype)
print(np.diff(datetimes))

So it looks like there's an np.datetime64 type wrapped around datetime from the standard library. Maybe I shouldn't have been trying to access System.DateTime? Perhaps there is already an np.datetime64 equivalent in Numpy.NET?

@henon
Copy link
Contributor Author

henon commented Mar 29, 2023

If you know how the date times are represented in memory in numpy (i.e. milliseconds since 1970 or whatever, I really don't know) then you can always use this workaround as described in #115 by creating a byte[] that contains the data which represent the datetimes you want. Of course that is a quite inconvenient workaround.

I'll look into it how I can support date time arrays, maybe I'll have to use the same workaround internally anyway.

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

2 participants