PythonNET DateTime C# .NET List how to populate it?

Viewed 2653

I can't find any answers out there for PythonNET working with Lists, a package for Python that lets you import .NET DLLs. For some reason creating a list of DateTime was not working for me. SOLVED Here's how to make it work:

import clr
from pandas import to_datetime, Series
from System.Collections.Generic import List
from System import DateTime

Contracts = to_datetime(Series(['9/1/2014','10/1/2014','11/1/2014','12/1/2014','1/1/2015','2/1/2015','3/1/2015','4/1/2015','5/1/2015','6/1/2015','7/1/2015','8/1/2015']))
DateList = List[DateTime](range(len(Contracts)))
for i in range(len(Contracts)): DateList.Add(DateTime(Contracts[i].year,Contracts[i].month,Contracts[i].day))

(Sorry about the above formatting, I can't get the indent to work in the loop). To show the contents of the list, you can do this:

print(DateList.get_Item(0))
9/1/2014 12:00:00 AM

print(DateList.get_Item(11))
8/1/2015 12:00:00 AM
2 Answers

This sample code shows how to pass integers, lists and a DateTime from Python to C# and back again.

It is tempting to go straight for the list of DateTimes, but its useful to get something simple working first before trying the answer from @Matt (and thanks for your excellent answer!).

Tested with:

  • Python 3.7.3
  • pythonnet 2.4dev0 from https://www.lfd.uci.edu/~gohlke/pythonlibs/
    • As of 2019-04-12, the Windows wheel is not yet released on PyPi, it will be released within a week or so.
  • .NET 4.7.2.
  • Also tested with .NET Standard 2.0, it works just as well.
  • I'm assuming compatibility with .NET Standard 3.0, when that is released.

C# code, compile after creating a Class project:

// Compile as "x32" or "x64" (depending on the bitness of your Python), but not "Any CPU".
using System;

namespace NetClass
{
    public class MyClass
    {
        public int XPlusOne(int x)
        {
            return x + 1;
        }

        public int[] XTimesTwo(int[] x)
        {
            int[] result = new int[x.Length];
            for (int i = 0; i < x.Length; i++)
            {
                result[i] = x[i] * 2;
            }
            return result;
        }

        public DateTime DatePlusOne(DateTime dateTime)
        {
            return dateTime.AddDays(1);
        }
    }
}

Python:

import clr
# Must compile your C# .dll in x64 or x32 (but not "Any CPU"), depending on the bitness of Python.
clr.AddReference('C:\\python\\NetClass\\NetClass\\bin\\x64\\Release\\NetClass.dll')
# Example above is for .NET 4.7.2. Same thing for .NET Standard 2.0.
from NetClass import MyClass
myClass = MyClass()

x_plus_one = myClass.XPlusOne(4)
print(x_plus_one)  # Prints 5

x_array = [1, 2, 3, 4]
x_array_times_two = list(myClass.XTimesTwo(x_array))    
print(x_array_times_two)  # Prints [ 2,4,6,8 ]

import datetime as dt
from System import DateTime
date = dt.date(year=2019, month=4, day=12)
d = myClass.DatePlusOne(DateTime(date.year, date.month, date.day))
date_plus_one = dt.date(d.Year, d.Month, d.Day)
print(date_plus_one)  # Prints 2019-04-13.

Output:

5             # Input into C# is a python int of 4
[2, 4, 6, 8]  # Input into C# is a python array [1,2,3,4]
2019-04-13    # Input into C# is a python date 2019-04-12
Related