Dividing the Date SFrame Graphlab column into three columns (Year Month)

Given graphlab SFrame where there is a column with dates, for example:

 +-------+------------+---------+-----------+ | Store | Date | Sales | Customers | +-------+------------+---------+-----------+ | 1 | 2015-07-31 | 5263.0 | 555.0 | | 2 | 2015-07-31 | 6064.0 | 625.0 | | 3 | 2015-07-31 | 8314.0 | 821.0 | | 4 | 2015-07-31 | 13995.0 | 1498.0 | | 3 | 2015-07-20 | 4822.0 | 559.0 | | 2 | 2015-07-10 | 5651.0 | 589.0 | | 4 | 2015-07-11 | 15344.0 | 1414.0 | | 5 | 2015-07-23 | 8492.0 | 833.0 | | 2 | 2015-07-19 | 8565.0 | 687.0 | | 10 | 2015-07-09 | 7185.0 | 681.0 | +-------+------------+---------+-----------+ [986159 rows x 4 columns] 

Is there an easy way in the graphlab / other python function to convert a Date column to Year | Month | Day?

 +-------+------+----+----+---------+-----------+ | Store | YYYY | MM | DD | Sales | Customers | +-------+------+----+----+---------+-----------+ | 1 | 2015 | 07 | 31 | 5263.0 | 555.0 | | 2 | 2015 | 07 | 31 | 6064.0 | 625.0 | | 3 | 2015 | 07 | 31 | 8314.0 | 821.0 | +-------+------------+---------+-----------+ [986159 rows x 4 columns] 

In pandas I can do this: What is the fastest way to retrieve a day, month and year from a specific date?

But converting SFrame to Panda to split the date and converting back to SFrame is quite difficult.

+5
source share
2 answers

You can also do this using the split-datetime method. This gives you a little more flexibility.

 sf.add_columns(sf['Date'].split_datetime(column_name_prefix = '')) 

The split_datetime method split_datetime is located in SArray (one SFrame column) and returns an SFrame, which you can then add back to the original data (basically 0 value)

+4
source

A quick and dirty way to do it

 sf['date2'] = sf['Date'].apply(lambda x: x.split('-')) sf = sf.unpack('date2') 

Another option is to convert the Date column to a date and time type, and then use the graphlab.SArray.split_datetime function.

+2
source

Source: https://habr.com/ru/post/1236272/


All Articles