Search code examples
pythondatetimedatetime-formatpytz

How to convert datetime to the right format to use the pytz module


If I have a bunch of datetime(UTC) in a column like this:

63058    01/22/2018 11:00:00
63059    01/22/2018 12:00:00
63060    01/22/2018 13:00:00
Name: date, Length: 63061, dtype: object

or this:

DatetimeIndex(['2007-12-21 17:00:00', '2007-12-21 18:00:00',
           '2007-12-21 19:00:00', '2007-12-21 20:00:00',
           '2007-12-21 21:00:00', '2007-12-23 20:00:00',
           '2007-12-23 22:00:00', '2007-12-23 23:00:00',
           '2007-12-24 00:00:00', '2007-12-24 01:00:00',
           ...
           '2018-01-22 04:00:00', '2018-01-22 05:00:00',
           '2018-01-22 06:00:00', '2018-01-22 07:00:00',
           '2018-01-22 08:00:00', '2018-01-22 09:00:00',
           '2018-01-22 10:00:00', '2018-01-22 11:00:00',
           '2018-01-22 12:00:00', '2018-01-22 13:00:00'],
          dtype='datetime64[ns]', length=63061, freq=None)

How can I convert the whole (list or series), whatever it's called to this format:

datetime(2016, 1, 24, 18, 0, 0, tzinfo=utc)

Because I need to use the pytz module to convert utc to eastern time.

I get this error: AttributeError: 'Index' object has no attribute 'astimezone' when I use astimezone().strftime() with my original data format. This is what I originally wanted to do, but now I encounter this problem. Thanks.


Solution

  • You can define a function to convert one of the string representations as follows:

    from datetime import datetime
    import pytz
    
    def date_from_string(s):
        "Convert ``s`` from a string to a datetime object in the UTC time zone."
        dt = datetime.strptime(s, '%Y-%m-%d %H:%M:%S')
        return dt.replace(tzinfo=pytz.utc)
    

    then you can use it with map in your constructor

    DatetimeIndex(map(date_from_string, [
           '2007-12-21 17:00:00', '2007-12-21 18:00:00',
           '2007-12-21 19:00:00', '2007-12-21 20:00:00',
           '2007-12-21 21:00:00', '2007-12-23 20:00:00',
           '2007-12-23 22:00:00', '2007-12-23 23:00:00',
           '2007-12-24 00:00:00', '2007-12-24 01:00:00',
           ...
           '2018-01-22 04:00:00', '2018-01-22 05:00:00',
           '2018-01-22 06:00:00', '2018-01-22 07:00:00',
           '2018-01-22 08:00:00', '2018-01-22 09:00:00',
           '2018-01-22 10:00:00', '2018-01-22 11:00:00',
           '2018-01-22 12:00:00', '2018-01-22 13:00:00']),
          dtype='datetime64[ns]', length=63061, freq=None)
    

    The docs have more info:

    datetime.strptime(..): https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior

    map(..): https://docs.python.org/2/library/functions.html#map