Machine_leaning_9
Machine_leaning_9

Reputation: 47

timestamp in pandas dataframe

I have a dataframe like this:

+---+---+----+
| ts| p1| p2 |
+---+---+------
|200|[1]| null|
|220|[2]| [2] |
|240|[3]| [4] |
|250|[3]| [4] |
+---+---+-----+

I need an output like this:

+---+---+----+
| ts_new| p1| p2 |
+---+---+------
|200|[1]| null|
|200|[2]| [2] |
|230|[3]| [4] |
|230|[3]| [4] |
+---+---+-----+

Basically I need a function which takes an input of ts say 200, and then at every interval of 30 units, replaces the value of ts if it is in the interval of 30 units.

Any help is highly appreciated.

Upvotes: 2

Views: 67

Answers (1)

Alex Watt
Alex Watt

Reputation: 937

I think what you are looking for is something like this:

binsize = 30
offset = 200
df["ts"] = ((df["ts"] - offset) // binsize) * binsize + offset

Upvotes: 2

Related Questions