Cannot convert from pandas._libs.tslibs.timestamps.Timestamp to datetime.datetime

2024/9/20 10:54:06

I´m trying to convert from pandas._libs.tslibs.timestamps.Timestamp to datetime.datetime but the change is not saved:

type(df_unix.loc[45,'LastLoginDate'])

OUTPUT: pandas._libs.tslibs.timestamps.Timestamp

type(df_unix.loc[45,'LastLoginDate'].to_pydatetime())

OUTPUT: datetime.datetime

df_unix.loc[45,'LastLoginDate'] = df_unix.loc[i,'LastLoginDate'].to_pydatetime()

type(df_unix.loc[45,'LastLoginDate'])

OUTPUT: pandas._libs.tslibs.timestamps.Timestamp
Answer

Suppose that you have this date:

some_date = pd.Timestamp("1970-01-01")
print(some_date)
Output: Timestamp('1970-01-01 00:00:00')

To transform it to datetime just do this:

some_date.to_pydatetime()

And you will get:

output: datetime.datetime(1970, 1, 1, 0, 0)

You can't use datetime.datetime(some_date) because the constructor of the datetime.datetime class takes 3 int numbers:

One for the year, one for the month and one for the day.

https://en.xdnf.cn/q/119499.html

Related Q&A

string index out of range Python, Django

im using Django to develop a web application. When i try and run it on my web form i am receiving string index out of range error. However, when i hardcode a dictionary in to a python test file it work…

PyQt UI event control segregation

I am a beginner in python but my OOPS concept from Java and Android are strong enough to motivate me in making some tool in python. I am using PyQt for developing the application. In my application th…

How to re-run process Linux after crash? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.This question does not appear to be about a specific programming problem, a software algorithm, or s…

Number of pairs

I am trying to write a code that takes m. a, a list of integers n. b, an integer and returns the number of pairs (m,n) with m,n in a such that |m-n|<=b. So far, Ive got this def nearest_pairs(a, b):…

xml.etree.ElementTree.ParseError: not well-formed

I have the following code:from xml.etree import ElementTreefile_path = some_file_pathdocument = ElementTree.parse(file_path, ElementTree.XMLParser(encoding=utf-8))If my XML looks like the following it …

Convert nested XML content into CSV using xml tree in python

Im very new to python and please treat me as same. When i tried to convert the XML content into List of Dictionaries Im getting output but not as expected and tried a lot playing around.XML Content<…

How to decode binary file with for index, line in enumerate(file)?

I am opening up an extremely large binary file I am opening in Python 3.5 in file1.py:with open(pathname, rb) as file:for i, line in enumerate(file):# parsing hereHowever, I naturally get an error beca…

how to install pyshpgeocode from git [duplicate]

This question already has answers here:The unauthenticated git protocol on port 9418 is no longer supported(10 answers)Closed 2 years ago.I would like to install the following from Git https://github.c…

How to export dictionary as CSV using Python?

I am having problems exporting certain items in a dictionary to CSV. I can export name but not images (the image URL).This is an example of part of my dictionary: new = [{ "name" : "pete…

Passing values to a function from within a function in python

I need to pass values from one function to the next from within the function.For example (my IRC bot programmed to respond to commands in the channel):def check_perms(nick,chan,cmd):sql = "SELECT …