change multiple columns in pandas dataframe to datetime

后端 未结 5 2013
无人及你
无人及你 2021-01-30 22:38

I have a dataframe of 13 columns and 55,000 rows I am trying to convert 5 of those rows to datetime, right now they are returning the type \'object\' and I need to transform thi

相关标签:
5条回答
  • 2021-01-30 22:55
    my_df[['column1','column2']] =     
    my_df[['column1','column2']].apply(pd.to_datetime, format='%Y-%m-%d %H:%M:%S.%f')
    

    Note: of course the format can be changed as required.

    0 讨论(0)
  • 2021-01-30 22:56

    If performance is a concern I would advice to use the following function to convert those columns to date_time:

    def lookup(s):
        """
        This is an extremely fast approach to datetime parsing.
        For large data, the same dates are often repeated. Rather than
        re-parse these, we store all unique dates, parse them, and
        use a lookup to convert all dates.
        """
        dates = {date:pd.to_datetime(date) for date in s.unique()}
        return s.apply(lambda v: dates[v])
    
    to_datetime: 5799 ms
    dateutil:    5162 ms
    strptime:    1651 ms
    manual:       242 ms
    lookup:        32 ms
    

    Source: https://github.com/sanand0/benchmarks/tree/master/date-parse

    0 讨论(0)
  • 2021-01-30 22:57

    You can use apply to iterate through each column using pd.to_datetime

    data.iloc[:, 7:12] = data.iloc[:, 7:12].apply(pd.to_datetime, errors='coerce')
    
    0 讨论(0)
  • 2021-01-30 23:07

    If you rather want to convert at load time, you could do something like this

    date_columns = ['c1','c2', 'c3', 'c4', 'c5']
    data = pd.read_csv('file_to_read.csv', parse_dates=date_columns)
    
    0 讨论(0)
  • 2021-01-30 23:16

    First you need to extract all the columns your interested in from data then you can use pandas applymap to apply to_datetime to each element in the extracted frame, I assume you know the index of the columns you want to extract, In the code below column names of the third to the sixteenth columns are extracted. you can alternatively define a list and add the names of the columns to it and use that in place, you may also need to pass the date/time format of the the DateTime entries

    import pandas as pd
    
    cols_2_extract = data.columns[2:15]
    
    data[cols_2_extract] = data[cols_2_extract].applymap(lambda x : pd.to_datetime(x, format = '%d %M %Y'))
    
    0 讨论(0)
提交回复
热议问题