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.

Datetime objects are converted to Timestamp without my consent.

See original GitHub issue

Python datetime.datetime object get converted inside pandas dataframes. This is not in all cases desired.

Code Sample

import numpy as np
import pandas as pd

rd = lambda : datetime(2017,1,np.random.randint(1,32))
datelist = [rd() for i in range(10)]
print(type(datelist[0])) # <type 'datetime.datetime'>

df = pd.DataFrame({"date" : datelist,
                   "y" : np.random.rand(10)})

print(type(df["date"][0])) # <class 'pandas._libs.tslib.Timestamp'>

dates = [d.to_pydatetime() for d in df["date"]]
print(type(dates[0])) # <type 'datetime.datetime'>

Creating a dataframe with a list of datetime objects converts them to pandas._libs.tslib.Timestamp. This is undesired. In this simple case it is at least possible to get back a datetime object by list comprehension, dates = [d.to_pydatetime() for d in df["date"]], which I would consider as an unnecessary step and makes the use of a DataFrame somehow obsolete.

Background:

The real problem with this behaviour is shown in the following. Any instance of a class that gets derived from datetime is also converted, hence loosing all its properties.

import numpy as np
import pandas as pd

class someobject(datetime):
    prop = "property"
    def __init__(self,*args,**kwargs):
        datetime.__init__(*args,**kwargs)

rd = lambda : someobject(2017,1,np.random.randint(1,32))
datelist = [rd() for i in range(10)]
print(type(datelist[0])) # <class '__main__.someobject'>
print(datelist[0].prop)  # property

df = pd.DataFrame({"date" : datelist,
                   "y" : np.random.rand(10)})

print(type(df["date"][0])) # <class 'pandas._libs.tslib.Timestamp'>
print(df["date"][0].prop)  # Error AttributeError: 'Timestamp' object has no attribute 'prop'

How to prevent the DataFrame to convert an object that I put into it to something else?
As a workaround, how to change someobject in the above such that it does not get converted automatically to something else?

[Here, python 2.7, pandas 0.20.1 are used]

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

6reactions
hrushikesh-dhumalcommented, Jul 1, 2019

df['date'].dt.to_pydatetime() does not work when assigning a column to dataframe, it will revert back to pandas._libs.tslibs.timestamps.Timestamp.

This also happens when groupby is used on datetime column.

2reactions
li-denniscommented, Jul 30, 2019

Are there any good solutions to this? Pandas/numpy timestamps and timedeltas seem to be extremely nonperformant

Can you post an example with timing?

Sure. Apologies for the lack of example. Here’s an excerpt from some bit of code I was profiling where this came up when using line profiler, with python 3.7:

When using pd.Timedelta and pd.Timestamp

Time  Per Hit   % Time  Line Contents
==============================================================
19.3     46.6                  slot_sub_duration = end_slot_dt - start_dt

When using datetime.datetime and datetime.timedelta

Time  Per Hit   % Time  Line Contents
==============================================================
0.7      6.7                  slot_sub_duration = end_slot_dt - start_dt

Sorry if the formatting broke, but the time went from 19.3 us to 0.7us per hit.

The attached SO post was the incorrect link, here’s the corrected one, and my numbers seem to agree with theirs: https://stackoverflow.com/a/29192601/805763

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to convert a Python datetime object into timestamp ...
1 Answer 1 ... time.strptime uses whatever timezone it inherited from the process that started the python process. Likely the timezone that was ......
Read more >
datetime — Basic date and time types — Python 3.11.1 ...
Source code: Lib/datetime.py The datetime module supplies classes for manipulating dates and times. While date and time arithmetic is supported, the focus ...
Read more >
Demystifying DateTime Manipulation in JavaScript - Toptal
Converting a string to a JavaScript date object is done in different ways. The Date object's constructor accepts a wide variety of date...
Read more >
Python timestamp to datetime and vice-versa - Programiz
In this article, you will learn to convert timestamp to datetime object and datetime object to timestamp (with the help of examples).
Read more >
How to convert timestamp string to datetime object in Python?
You can simply use the fromtimestamp function from the DateTime module to get a date from a UNIX timestamp. This function takes the...
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