-1

i have a strange problem.

I get some datas like this :

save_last_row = exchange.fetch_ohlcv('BTC/USD', timeframe='1m', limit=2)
save_last_row = pd.DataFrame(save_last_row[:-1],columns=['Date Time', 'Open', 'High', 'Low', 'Close', 'Volume'])

The data looks like that :

       Date Time     Open     High      Low    Close       Volume
0  1640190840000  49075.0  49085.0  49044.0  49071.0  147792.0244

It is a loop so i get the last line every loop. Strangely, when i do that after :

last_row = save_last_row
last_row = function_convert(last_row)

i obtain that for last_row :

            Date Time     Open     High      Low    Close       Volume
0 2021-12-22 17:41:00  49041.0  49073.0  49029.0  49033.0  136169.2562

We can see the date time is not in the same format. And if i printf the save_last_row (that i didn't modify), i have :

            Date Time     Open     High      Low    Close       Volume
0 2021-12-22 17:41:00  49041.0  49073.0  49029.0  49033.0  136169.2562

WHY ???? Last_row get just the data from save_last_row. how the variable save_last_row could be modify ???

NB : and the function_convert just modify some values :

def conversion_date(df):
  df["Date Time"] = df["Date Time"] + 3600000 # add one hour to have local time
  df["Date Time"] = (df["Date Time"].apply(parse_dates)) # convert
  return df

EDIT : the variable last_row is converted (ok), but save_last_row also !!! (and it is not ok, i didn't convert it) so why ??

EDIT2 : use save_last_row.copy() thank you Chris Doyle

Skykron
  • 21
  • 1
  • 1
  • 6
  • is `parse_dates` something we should know? is a built-in function somewhere? what you originally have is a Unix time with millisecond precision, just from a glance. `parse_dates` is somehow converting that. – mechanical_meat Dec 22 '21 at 16:52
  • def parse_dates(ts): return datetime.fromtimestamp(ts/1000.0) – Skykron Dec 22 '21 at 16:55
  • 3
    why are you surprised that the date format changes when you have a date confersion function....... – Chris Doyle Dec 22 '21 at 17:00
  • I think your confusion is that `last_row = save_last_row` does not make a copy of the data frame, it just says last_row and save_last_row now point at the same data frame. So if you modify last_row, your modifying the same data frame that save_last_row points to. Instead maybe you wanted `last_row = save_last_row.copy()` – Chris Doyle Dec 22 '21 at 17:12
  • ohhhhhhhhhhhh understand (is not working like C/C++) – Skykron Dec 22 '21 at 17:12
  • some objects in python are immutable, but dataframes are mutable, so just assigning it to another name doesnt create a copy it just creates another name that points to the underlying dataframe. – Chris Doyle Dec 22 '21 at 17:14
  • @ChrisDoyle assigning to another name **never makes a copy** regardless of the *type* of the object – juanpa.arrivillaga Dec 22 '21 at 17:16
  • 1
    Probably should just be closed as a duplicate of: https://stackoverflow.com/questions/2612802/list-changes-unexpectedly-after-assignment-why-is-this-and-how-can-i-prevent-it – juanpa.arrivillaga Dec 22 '21 at 17:17

1 Answers1

0

If I understand you correctly you want the timestamp as an integer and not as a datetime object.

Your parse_date function parses the integer timestamp to a datetime object, which is presented differently. If you don't want that conversion just leave the second line from your conversion_date function like that.

def conversion_date(df):
  df["Date Time"] = df["Date Time"] + 3600000 # add one hour to have local time
  return df
jstnklnr
  • 84
  • 4