question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add nanosecond attribute to DateTime for compatibility with pandas

See original GitHub issue

Constructing a DataFrame from pendulum DateTime fails because of missing nanosecond attribute:

import pandas
import pendulum
pandas.DataFrame({'time': [pendulum.now()]})

...
  File "pandas/_libs/tslibs/conversion.pyx", line 178, in pandas._libs.tslibs.conversion.datetime_to_datetime64
  File "pandas/_libs/tslibs/conversion.pyx", line 387, in pandas._libs.tslibs.conversion.convert_datetime_to_tsobject
AttributeError: 'DateTime' object has no attribute 'nanosecond'

However suing naive() it works:

pandas.DataFrame({'time': [pendulum.now().naive()]})
Out[5]: 
                     time
0  2018-06-29T13:30:30.540966

Would it be possible to add the nanosecond attribute to DateTime class, event if it always returns 0 for now?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
liquidgeniuscommented, Feb 21, 2019

Issue Example

import pendulum
import pandas as pd

start = pendulum.datetime(2018, 4, 17)
end = pendulum.datetime(2018, 4,25)

period = pendulum.period(start, end)

period_days = [dt for dt in period.range('days')]
period_days = pd.Series(period_days)

Console Output

python3.6:
>     922                 from pandas import DatetimeIndex
>     923 
> --> 924                 values, tz = conversion.datetime_to_datetime64(v)
>     925                 return DatetimeIndex(values).tz_localize(
>     926                     'UTC').tz_convert(tz=tz)
> 
> pandas/_libs/tslibs/conversion.pyx in pandas._libs.tslibs.conversion.datetime_to_datetime64()
> pandas/_libs/tslibs/conversion.pyx in pandas._libs.tslibs.conversion.convert_datetime_to_tsobject()
> AttributeError: 'DateTime' object has no attribute 'nanosecond'

It appears as though pandas converts the datetime to a datetime64 behind the scenes, and validation of the datetime is throwing an exception.

Solution

You can manually reformat the date without the nanonseconds so that it passes pandas validation:

import pendulum
import pandas as pd

def pendulum_to_pandas(pend_dt):
    """Converts a Pendulum datetime to a Pandas datetime

    Parameters
    -----------
    pend_dt (Pendulum.datetime): Any Pendulum datetime. Pendulum datetimes include 
        nanoseconds that Pandas does not support.

    Returns
    --------
    results (Pandas friendly datetime): A Pandas friendly datetime excluding nanoseconds.
    """
    
    # Drop nanoseconds
    results = pend_dt.strftime("%Y-%m-%d %H:%M:%S %z")
    
    return results

Usage

# Create a pendulum datetime for the current point in time
now = pendulum.now()

# Convert to pandas friendly datetime
pandas_friendly_datetime = pendulum_to_pandas(now)

Console Output

> 2018-04-17 00:00:00 +0000

Alternatively with a list comprehension

start = pendulum.datetime(2018, 4, 17)
end = pendulum.datetime(2018, 4,25)

period = pendulum.period(start, end)

period_days = [pendulum_to_pandas(dt) for dt in period.range('days')]
period_days = pd.Series(period_days)

Console Output

> 0    2018-04-17 00:00:00 +0000
> 1    2018-04-18 00:00:00 +0000
> 2    2018-04-19 00:00:00 +0000
> 3    2018-04-20 00:00:00 +0000
> 4    2018-04-21 00:00:00 +0000
> 5    2018-04-22 00:00:00 +0000
> 6    2018-04-23 00:00:00 +0000
> 7    2018-04-24 00:00:00 +0000
> 8    2018-04-25 00:00:00 +0000

This said, It would just be nice to have both tools work happily together. @jwkvam @Dmitrii-I

0reactions
liquidgeniuscommented, Feb 21, 2019
    def pendulum_datetimes(data, pandas_friendly=True):
        """
        Converts all standard library datetimes in a Dictionary to Pendulum datetime instances. 
        Default is to return a Pandas friendly Pendulum datetime.
        
        Parameters
        ----------
        data (Dictionary): Dictionary to be parsed for Pendulum datetime conversion.
        pandas_friendly (Boolean): Ensure pandas compatibility by excluding nanoseconds,
            defaults to True.

        Returns
        -------
        data (Dictionary): Updated record with Pendulum (and possibly Pandas friendly)
            datetimes.

        """
        
        # Iterate key, value pairs
        for key, value in data.items():
        
            # Check every field for datetimes
            if type(value) is datetime.datetime:

                # Convert datetimes to Pendulum datetimes
                pendulum_datetime = pendulum.instance(value)
                
                # Ensure Pandas compatibility (loses nanoseconds, see here: http://bit.ly/2RH9hwk )
                if pandas_friendly:
                    data[key] = pendulum_datetime.strftime("%Y-%m-%d %H:%M:%S %z")
                else:
                     data[key] = pendulum_datetime
    
        return data
Read more comments on GitHub >

github_iconTop Results From Across the Web

pandas out of bounds nanosecond timestamp after offset ...
Since pandas represents timestamps in nanosecond resolution, the timespan that can be represented using a 64-bit integer is limited to ...
Read more >
pandas.Timestamp — pandas 1.5.2 documentation
Pandas replacement for python datetime.datetime object. Timestamp is the pandas equivalent of python's Datetime and is interchangeable with it in most cases.
Read more >
Time series / date functionality — pandas 1.5.2 documentation
Date times: A specific date and time with timezone support. Similar to datetime.datetime from the standard library. Time deltas: An absolute time duration....
Read more >
pandas.Timestamp — pandas 0.25.0 documentation
Pandas replacement for python datetime.datetime object. Timestamp is the pandas equivalent of python's Datetime and is interchangeable with it in most cases.
Read more >
Time series / date functionality - Pandas
pandas supports converting integer or float epoch times to Timestamp and DatetimeIndex . The default unit is nanoseconds, since that is how Timestamp...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found