3

I have a series called "template" that is 3600 $0.00's (one $0.00 for each second in the last hour, and indexed as such). I have another series called "transactions" that comes from data from an API request that is a variable number of non-zero dollar amounts, also indexed by timestamps. Since these transactions are all made within the last hour, all timestamps in "transactions" will be found in "template," but not vice versa (unless there was a transaction every second of the last hour).

For each timestamp in "template" that matches a timestamp in "transactions", I want to replace that $0.00 with the dollar amount in "transactions." I'm doing this to generate a plot of transactions in the last hour, hence the need for $0.00's instead of NaN's, and 3600 data points (to maintain the consistency of the time axis of the graph).

I thought for sure it was as simple as:

graphSeries = template.update(transactions)

but when I run

print(template)
print(transactions)
print(graphSeries)

I get this (the "None" at the end being the issue):

12-26-2021 21:30:39    0.0
12-26-2021 21:30:40    0.0
12-26-2021 21:30:41    0.0
12-26-2021 21:30:42    0.0
12-26-2021 21:30:43    0.0
                      ...
12-26-2021 22:30:34    0.0
12-26-2021 22:30:35    0.0
12-26-2021 22:30:36    0.0
12-26-2021 22:30:37    0.0
12-26-2021 22:30:38    0.0
Length: 3600, dtype: float64

12-26-2021 21:31:10      697986.00
12-26-2021 21:32:07     8780106.00
12-26-2021 21:32:54      900542.00
12-26-2021 21:34:52     8784483.00
12-26-2021 21:35:05    26309692.00
12-26-2021 21:39:48      500000.00
12-26-2021 21:39:55     1999940.00
12-26-2021 21:41:54     1189745.40
12-26-2021 21:43:21     1399102.56
12-26-2021 21:44:33     1607717.00
12-26-2021 21:46:13     1189745.40
12-26-2021 21:47:56     1000000.00
12-26-2021 21:49:08    12414533.00
12-26-2021 21:50:21     1546034.00
12-26-2021 21:50:30    13141109.60
12-26-2021 21:50:33     2277359.00
12-26-2021 21:50:39      503834.00
12-26-2021 21:50:51     1397967.00
12-26-2021 21:51:03      580000.00
12-26-2021 21:51:44      542158.00
12-26-2021 21:52:12     1546034.00
12-26-2021 21:54:04     1662123.80
12-26-2021 21:54:31    12052320.00
12-26-2021 21:54:52     1546024.00
12-26-2021 21:55:53     1000000.00
12-26-2021 21:56:30     1507000.00
dtype: float64

None

Am I doing something wrong? Does pandas' update(Series) method not index with timestamps?

AnthonyJS
  • 153
  • 1
  • 3
  • 10
  • 2
    Can you create a simple example of your problem using like 5 or six rows. https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples – Scott Boston Dec 27 '21 at 03:48
  • 1
    Thank you @ScottBoston for the guide on forming better questions. I will use this from now on, however in this example, the answer from Paul below answers my question. – AnthonyJS Dec 28 '21 at 02:31

1 Answers1

1

Update is an inplace function, try:

template.update(transactions)
print(template)

More info on update: https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.update.html

Paul
  • 1,801
  • 1
  • 12
  • 18
  • Ah, thank you very much for this answer! Is there a way to do this in pandas using something other than .update(series) that is not an inplace function? For reference to other viewers of this question, here is the pandas.Series doc (https://pandas.pydata.org/docs/reference/api/pandas.Series.update.html?highlight=update#pandas.Series.update) – AnthonyJS Dec 28 '21 at 02:30
  • The reason I asked is because I need to reuse my template series. I suppose I could just do template_copy = template.copy() as needed, and update() the copies. – AnthonyJS Dec 28 '21 at 02:37
  • 1
    I think copying is a reasonable solution – Paul Dec 28 '21 at 07:58