0

A requirement I was given was to change the dates (not times) of a pandas dataframe. However, I am unable to change the dates of the given pandas date column and encountered a few errors

I did some exploring and discovered the ".replace" keyword for pandas. However, when I used that keyword for the following code:

report_time = df['report_time']

report_time.Timestamp.replace(year = 2022, month = 3, day = 21)

And I get the following error:

Traceback (most recent call last):
  File "/Users/kenneth/Desktop/hardware_test_harness/readCSV.py", line 20, in <module>
    report_time.Timestamp.replace(year = 2022, month = 3, day = 21)
  File "/usr/local/lib/python3.9/site-packages/pandas/core/generic.py", line 5583, in __getattr__
    return object.__getattribute__(self, name)
AttributeError: 'Series' object has no attribute 'Timestamp'

Here is a screenshot of my pandas dataframe, as well as copy of my code. I'm incredibly lost on what I need to do here to make this a success.

import pandas as pd
import numpy as np
import datetime 
from datetime import datetime
from datetime import timedelta

#todo: 
#write a script that performs an installation of all the needed files
df = pd.read_csv('event_file.csv')

#5 columns, pandas starts from 0
zone_type = df['zone_type'].to_numpy()
reported_time = df['report_time'].to_numpy()
event_type = df['event_type'].to_numpy()
value = df['value'].to_numpy()


#report_time = df['report_time']
#report_time.Timestamp.replace(year = 2022, month = 3, day = 21)

print(df)

  • 1
    `replace` method is not vectorized but you can `apply` it; e.g. like `report_time.apply(lambda t: t.replace(year = 2022, month = 3, day = 21))` – FObersteiner Mar 21 '22 at 22:34
  • What do you mean by change? You want to add/remove certain time to the column? – MSH Mar 21 '22 at 22:34
  • @FObersteiner got it, thank you so much for your suggestion! I went ahead and attempted that solution you've put and it appears that I got another issue, here's the error output https://pastebin.com/ZWP0ePdE – Kenneth Chen Mar 21 '22 at 22:54
  • @MSH Yeah, perhaps like changing the date, or adding the "future range" of dates based on the time. E.g.: if the csv spans for 8 days (and the initial date was in 1990), I'd like to reflect that with the date changes. I'm not sure if that makes sense but I'd love to clarify if it sounds confusing! – Kenneth Chen Mar 21 '22 at 22:55
  • ah sorry, I was assuming you already have converted the column to datetime type. You'll have to do that first; e.g. `report_time = pd.to_datetime(df['report_time'])` - depending how the date is formatted in the input string, you'll need to add keyword `dayfirst=True` or a specific `format` – FObersteiner Mar 21 '22 at 22:58

0 Answers0