Microsoft.Data.Analysis DataFrame - What is the idiomatic way to fill one column with manipulated values from another?

Chris X 41 Reputation points
2022-04-27T13:40:16.503+00:00

C# 10 / .NET 6 / Microsoft.Data.Analysis

Given a Microsoft.Data.Analysis DataFrame with two columns, what is the idiomatic way to take values from one column, manipulate them, then use the resulting value to fill the rows of the second column element-wise?

    // Create a DateTime column.
    PrimitiveDataFrameColumn<DateTime> dateTimeCol = new("Dates", 0);
    // Fill it.
    dateTimeCol.Append(DateTime.Now + TimeSpan.FromDays(1));
    dateTimeCol.Append(DateTime.Now + TimeSpan.FromDays(2));
    dateTimeCol.Append(DateTime.Now + TimeSpan.FromDays(3));

    // Create a Ticks column.
    PrimitiveDataFrameColumn<long> ticksCol = new("Ticks", dateTimeCol.Length);

    // Create a DataFrame of the above two columns.
    DataFrame df = new();
    df.Columns.Add(dateTimeCol);
    df.Columns.Add(ticksCol);

At this point, what I want to do is df["Ticks"] = df["Dates"].Ticks. Of course, this doesn't work. I could do this:

    for (int i = 0; i < df.Rows.Count; i++)
    {
        DateTime tempDate = (DateTime) df[i, df.Columns.IndexOf("Dates")];
        df[i, df.Columns.IndexOf("Ticks")] = tempDate.Ticks;
    }

But... is there a better way?

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,573 questions
.NET Machine learning
.NET Machine learning
.NET: Microsoft Technologies based on the .NET software framework.Machine learning: A type of artificial intelligence focused on enabling computers to use observed data to evolve new behaviors that have not been explicitly programmed.
154 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jack J Jun 24,491 Reputation points Microsoft Vendor
    2022-04-28T05:45:07.26+00:00

    @Chris X , Welcome to Microsoft Q&A, based on my research, I find that there is no direct way to give the value like you mentioned.

    We could only use loop to do it. Maybe the following code will be more simpler.

    var list = df["Dates"].Cast<DateTime>().Select(i => i.Ticks).ToList();  
      
    for (int i = 0; i < list.Count; i++)  
    {  
        df["Ticks"][i] = list[i];  
    }  
    

    Hope this could help you.

    Best Regards,
    Jack


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful