I have a huge dataset of around 20gb. I have read the data using graphlab.SFrame.read_csv(). I have a date column which is read as string in the format yyyy-dd-mm. But i want the column to be read as a datetime object. How can I do it?
I understand that one way is to iterate through each row and change it using python code. Is there any other way? May be faster?
We can convert a string to datetime using strptime() function. This function is available in datetime and time modules to parse a string to datetime and time objects respectively.
Use pandas.to_datetime() method is used to change String/Object time to date type (datetime64[ns]). This method is smart enough to change different formats of the String date column to date.
Import the datetime library. Use the datetime. datetime class to handle date and time combinations. Use the strptime method to convert a string datetime to a object datetime.
yyyy-mm-dd stands for year-month-day . We can convert string format to datetime by using the strptime() function. We will use the '%Y/%m/%d' format to get the string to datetime.
There's actually a built-in method for this in graphlab.SArray
. Like Greg Whittier's answer, suppose your original date column is called datestring
.
import graphlab
sf = graphlab.SFrame.read_csv('input.csv')
sf['datetime'] = sf['datestring'].str_to_datetime('%Y-%d-%m')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With