Python: convert 'days since 1990' to datetime object

前端 未结 3 1618
长情又很酷
长情又很酷 2021-02-20 02:04

I have a time series that I have pulled from a netCDF file and I\'m trying to convert them to a datetime format. The format of the time series is in \'days since 1990-01-01 00:0

相关标签:
3条回答
  • 2021-02-20 02:26

    netCDF num2date is the correct function to use here:

    import netCDF4
    
    ncfile = netCDF4.Dataset('./foo.nc', 'r')
    time = ncfile.variables['time'] # do not cast to numpy array yet 
    time_convert = netCDF4.num2date(time[:], time.units, time.calendar)
    

    This will convert number of days since 1900-01-01 (i.e. the units of time) to python datetime objects. If time does not have a calendar attribute, you'll need to specify the calendar, or use the default of standard.

    0 讨论(0)
  • 2021-02-20 02:31

    The datetime module's timedelta is probably what you're looking for.

    For example:

    from datetime import date, timedelta
    
    days = 9465                 # This may work for floats in general, but using integers
                                #   is more precise (e.g. days = int(9465.0))
    
    start = date(1990,1,1)      # This is the "days since" part
    
    delta = timedelta(days)     # Create a time delta object from the number of days
    
    offset = start + delta      # Add the specified number of days to 1990
    
    print(offset)               # >>>  2015-12-01
    print(type(offset))         # >>>  <class 'datetime.date'>
    

    You can then use and/or manipulate the offset object, or convert it to a string representation however you see fit.

    You can use the same format as for this date object as you do for your time_datetime:

    print(offset.strftime('%Y-%m-%d %H:%M:%S'))
    

    Output:

    2015-12-01 00:00:00
    

    Instead of using a date object, you could use a datetime object instead if, for example, you were later going to add hours/minutes/seconds/timezone offsets to it.

    The code would stay the same as above with the exception of two lines:

    # Here, you're importing datetime instead of date
    from datetime import datetime, timedelta
    
    # Here, you're creating a datetime object instead of a date object
    start = datetime(1990,1,1)   # This is the "days since" part
    

    Note: Although you don't state it, but the other answer suggests you might be looking for timezone aware datetimes. If that's the case, dateutil is the way to go in Python 2 as the other answer suggests. In Python 3, you'd want to use the datetime module's tzinfo.

    0 讨论(0)
  • 2021-02-20 02:35

    We can do this in a couple steps. First, we are going to use the dateutil library to handle our work. It will make some of this easier.

    The first step is to get a datetime object from your string (1990-01-01 00:00:00 +10). We'll do that with the following code:

    from datetime import datetime
    from dateutil.relativedelta import relativedelta
    import dateutil.parser
    
    days_since = '1990-01-01 00:00:00 +10'
    days_since_dt = dateutil.parser.parse(days_since)
    

    Now, our days_since_dt will look like this:

    datetime.datetime(1990, 1, 1, 0, 0, tzinfo=tzoffset(None, 36000))
    

    We'll use that in our next step, of determining the new date. We'll use relativedelta in dateutils to handle this math.

    new_date = days_since_dt + relativedelta(days=9465.0)
    

    This will result in your value in new_date having a value of:

    datetime.datetime(2015, 12, 1, 0, 0, tzinfo=tzoffset(None, 36000))
    

    This method ensures that the answer you receive continues to be in GMT+10.

    0 讨论(0)
提交回复
热议问题