Pandas Python: KeyError Date

2024/11/20 13:30:42

I am import into python where it will automatically create a date time object.

However I want the first column to be a datetime object in Python. Data looks like

Date,cost
41330.66667,100
41331.66667,101
41332.66667,102
41333.66667,103

Current code looks like:

from datetime import datetime
import pandas as pddata = pd.read_csv(r"F:\Sam\PJ\CSV2.csv")
data['Date'].apply(lambda x: datetime.strptime(x, '%d/%m/%Y'))print(data)
Answer

This looks like an excel datetime format. This is called a serial date. To convert from that serial date you can do this:

data['Date'].apply(lambda x: datetime.fromtimestamp( (x - 25569) *86400.0))

Which outputs:

>>> data['Date'].apply(lambda x: datetime.fromtimestamp( (x - 25569) *86400.0))
0   2013-02-25 10:00:00.288
1   2013-02-26 10:00:00.288
2   2013-02-27 10:00:00.288
3   2013-02-28 10:00:00.288

To assign it to data['Date'] you just do:

data['Date'] = data['Date'].apply(lambda x: datetime.fromtimestamp( (x - 25569) *86400.0))#dfDate  cost
0 2013-02-25 16:00:00.288   100
1 2013-02-26 16:00:00.288   101
2 2013-02-27 16:00:00.288   102
3 2013-02-28 16:00:00.288   103
https://en.xdnf.cn/q/119826.html

Related Q&A

How to resample 1 minute data into 15 minute data?

CSV file. df before resample and after applying: df["dateandtime"] = (pd.to_datetime(df.pop("DATE").str.cat(df.pop("TIME"), sep=" "))) df = df.set_index(pd.Datet…

Python Pygame Lighting for Pong

Hey Guys Im writing a little Pong-Game in Pygame and wanted to use a glowing-effect on the Ball and the Bats. But Pygame dosent support this effects and make solid blocks out of it. Is there a way to h…

python - List.remove method not applicable as the function with the map builtin?

Question Can List.remove not be used as the function to apply in the map(function, iterable, ...) builtin? %%timeit import random m = _max = 10000 n = random.randint(1, m)outer = random.sample(populat…

How do I change a sum for string for it to work?

This is my whole program Im working on (with the function not in the right place due to the code needing to be indented) but anyway there is a problem that Im not sure how to fix.How do I change it so …

Convert type str to numerator/ denominator

Im having some trouble converting type str to numbers. I use a separate text-file containing the following numbers 1, 2, 3, 4, 5, 6 and then I import these numbers into python and save them as a list. …

discord.py MemberNotFound exception when passing a real member

When I pass *grant @user add in Discord I get the following exception: Ignoring exception in command grant: Traceback (most recent call last):File "/Users/test/PycharmProjects/slapdash/venv/lib/py…

using argument end= for print function in Python 3.3

Consider the following code:i=0 while i<5:print(i, end=" ")i = i + 1which results in the output:0 1 2 3 4if i want to add a string "the result" before 1 2 3 4, output expected: t…

Get the max value on a list of tuples

I have a list of tuples:card_list= [(2, (1, S)), (0, (12, H)), (1, (5, C)]This list contains cards: (cardindex, (value, suit)) where cardindex is a index to store the position of the card but irrelevan…

Naming of file while saving with extension

How can I save a file with the name as: oldname+"_new"+extension in an elegant way? I currently do: ext = os.path.splitext(file)[1] output_file = (root+/+ os.path.splitext(file)[0]+"_ne…

Unique permutations of a list without repetition

I understand there are many, many posts about permutations (unique, variable length, etc.), but I have not been able to use them to solve my particular issue.Lets say I have a list of metropolitan area…