Message Boards Message Boards


How to convert string to date object to sort the data ?

Posted 4 months ago
3 Replies
3 Total Likes

Hello, I have been struggling with how to sort my data by date.
I have a CSV file with one attribute as date. I have tried importing the file using Sematic Import in order to obtain the date data as a date object but it has been futile.
Here is an example of my data:
A.Name 2022-01-01 1:02 Subject Message
Whenever i import the data as a dataset, it is imported as a string. How can i convert it into a date object so that i can sort the entire dataset by date.
I have lost hope and will try to use some other language if this is not possible with Mathematica.
Any help would be much appreciated. Been struggling with this for 4 weeks.

POSTED BY: Rashmi Dhakad
3 Replies
Posted 4 months ago

Hi Rashmi,

Could you please attach a sample (first 10 rows) of your CSV file to your question. Not obvious why SemanticImport would fail.

do = DateObject["2022-01-01 1:02"]
do // InputForm
(* DateObject[{2022, 1, 1, 1, 2}, "Minute", "Gregorian", -6.] *)
POSTED BY: Rohit Namjoshi
Posted 4 months ago

POSTED BY: Eric Rimbey
Posted 4 months ago

Alternatively, using Eric's sample data file

rawData = Import["~/sampledata.csv", "Dataset", HeaderLines -> 1]
data = rawData[All, <|#, "date" -> DateObject[#date]|> &]

To sort by the date column

POSTED BY: Rohit Namjoshi
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract