Search code examples
pandasdataframedaskstring-to-datetime

Efficient Dataframe column (Object) to DateTime conversion


I'm attempting to create a new column that contains the data of the Date input column as a datetime. I'd also happily accept changing the datatype of the Date column, but I'm just as unsure how to to that.

I'm currently using DateTime = dd.to_datetime. I'm importing from a CSV and letting dask decide on data types.

I'm fairly new to this, so I've tried a few stackoverflow answers, but I'm just fumbling and getting more errors than answers.

My input date string is, for example:

2019-20-09 04:00

This is what I currently have,

import dask.dataframe as dd
import dask.multiprocessing
import dask.threaded
import pandas as pd

# Dataframes implement the Pandas API
import dask.dataframe as dd

ddf = dd.read_csv(r'C:\Users\i5-Desktop\Downloads\State_Weathergrids.csv')
print(ddf.describe(include='all'))
ddf['DateTime'] = dd.to_datetime(ddf['Date'], format='%y-%d-%m %H:%M')

The error I'm receiving is below. I 'm assuming that the last line is the most relevant piece, but for the life of me I cannot work out why the date format given doesn't match the format I'm specifying.

TypeError                                 Traceback (most recent call last)
~\Anaconda3\lib\site-packages\pandas\core\tools\datetimes.py in _convert_listlike_datetimes(arg, box, format, name, tz, unit, errors, infer_datetime_format, dayfirst, yearfirst, exact)
    290             try:
--> 291                 values, tz = conversion.datetime_to_datetime64(arg)
    292                 return DatetimeIndex._simple_new(values, name=name, tz=tz)

pandas/_libs/tslibs/conversion.pyx in pandas._libs.tslibs.conversion.datetime_to_datetime64()

TypeError: Unrecognized value type: <class 'str'>

During handling of the above exception, another exception occurred:
....
ValueError: time data '2019-20-09 04:00' does not match format '%y-%d-%m %H:%M' (match)

Data Frame current properties using describe:

Dask DataFrame Structure:
              Location    Date Temperature       RH  
npartitions=1                                                                                           
               float64  object     float64  float64  
                   ...     ...         ...      ...      
Dask Name: describe, 971 tasks

Sample Data

+-----------+------------------+-------------+--------+
|  Location |       Date       | Temperature |   RH   |
+-----------+------------------+-------------+--------+
|      1075 | 2019-20-09 04:00 | 6.8         | 99.3   |
|      1075 | 2019-20-09 05:00 | 6.4         | 100.0  |
|      1075 | 2019-20-09 06:00 | 6.7         | 99.3   |
|      1075 | 2019-20-09 07:00 | 8.6         | 95.4   |
|      1075 | 2019-20-09 08:00 | 12.2        | 76.0   |
+-----------+------------------+-------------+--------+

Solution

  • Try this,

    ['DateTime'] = dd.to_datetime(ddf['Date'], format='%Y-%d-%m %H:%M', errors = 'ignore')

    errors ignore will return Nan wherever to_datetime fails..

    For more detail visit https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.to_datetime.html