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 creating string ND array #50

Closed
19csi opened this issue Jul 21, 2020 · 7 comments
Closed

Error creating string ND array #50

19csi opened this issue Jul 21, 2020 · 7 comments

Comments

@19csi
Copy link

19csi commented Jul 21, 2020

Hi,

I'm trying to create an NDarray to store DateTime values from a normal C# array. When I try to create the NDarray I get the following error: Can not convert type of given object to dtype: System.String[].

            string[] times = new string[50];

            DateTime now = DateTime.Now;

            for (int i = 0; i < 50; i++)            
                times[i] = now.AddSeconds(i).ToString();            

            // Raises Exception
            NDarray<string> nTimes = np.array<string>(times);

When I try to make a NDarray<float> it works fine, but for NDarray<DateTime> and NDarray<string> this error occurs. Is there something I am doing wrong here or are these types not supported? Thanks for the help!

@henon
Copy link
Contributor

henon commented Jul 21, 2020

Yeah, DateTime and String can't simply be marshalled into Python like Integers or Floats, so I left support for them out for now. I'll look into it. Hope it is not too complicated.

@19csi
Copy link
Author

19csi commented Jul 21, 2020

Gotcha, that's what I was thinking but wanted to make sure. Thanks for the quick response.

@henon
Copy link
Contributor

henon commented Jul 22, 2020

with a little modification an array of strings is already working:

        [TestMethod]
        public void StringArray()
        {
            //>>> a = numpy.array(['apples', 'foobar', 'cowboy'])
            //>>> a[2] = 'bananas'
            //>>> a
            //array(['apples', 'foobar', 'banana'], 
            //      dtype = '|S6')
            var a = np.array(new string[]{"apples", "foobar", "cowboy"});
            Assert.AreEqual("array(['apples', 'foobar', 'cowboy'], dtype='<U6')", a.repr);
            // todo: a[2]="banana";
            a.self.SetItem(new PyInt(2), new PyString("banana"));
            Assert.AreEqual("array(['apples', 'foobar', 'banana'], dtype='<U6')", a.repr);

            //>>> a = numpy.array(['apples', 'foobar', 'cowboy'], dtype = object)
            //>>> a
            //array([apples, foobar, cowboy], dtype = object)
            //>>> a[2] = 'bananas'
            //>>> a
            //array([apples, foobar, bananas], dtype = object)
            a = np.array(new string[] { "apples", "foobar", "cowboy" }, dtype:np.object_);
            Assert.AreEqual("array(['apples', 'foobar', 'cowboy'], dtype=object)", a.repr);
            // todo: a[2]="banana";
            a.self.SetItem(new PyInt(2), new PyString("banana"));
            Assert.AreEqual("array(['apples', 'foobar', 'banana'], dtype=object)", a.repr);
        }

is that enough for your use case?

@henon
Copy link
Contributor

henon commented Jul 22, 2020

in any case, I released the change as v21 to nuget

@19csi
Copy link
Author

19csi commented Jul 22, 2020

Awesome, that works for me! That was fast too 👍

@19csi 19csi closed this as completed Jul 22, 2020
@amine-aboufirass
Copy link

amine-aboufirass 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)
});

@henon
Copy link
Contributor

henon commented Mar 29, 2023

@amine-aboufirass I created a separate issue for your request: #119

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

3 participants