Python Forum
Elegant way to apply each element of an array to a dataframe?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Elegant way to apply each element of an array to a dataframe?
#6
This gives you the first and last time for each day. Does it solve your problem?
iimport pandas as pd
from datetime import datetime, timedelta
from time import time

now = datetime.now()
df = pd.DataFrame({"time": [now + timedelta(seconds=x) for x in range(930000)]})
start = time()
df["day"] = df.time.dt.day
df2 = df[df.day.shift(1) != df.day.shift(-1)]
print(time() - start)
print(df2)
Output:
0.0659632682800293 time day 0 2024-03-28 16:26:10.637194 28 27229 2024-03-28 23:59:59.637194 28 27230 2024-03-29 00:00:00.637194 29 113629 2024-03-29 23:59:59.637194 29 113630 2024-03-30 00:00:00.637194 30 200029 2024-03-30 23:59:59.637194 30 200030 2024-03-31 00:00:00.637194 31 286429 2024-03-31 23:59:59.637194 31 286430 2024-04-01 00:00:00.637194 1 372829 2024-04-01 23:59:59.637194 1 372830 2024-04-02 00:00:00.637194 2 459229 2024-04-02 23:59:59.637194 2 459230 2024-04-03 00:00:00.637194 3 545629 2024-04-03 23:59:59.637194 3
Another approach is to extract the day as above, then group the dataframe by day. You could compute the high, low, nean, open, close for each day.
Reply


Messages In This Thread
RE: Elegant way to apply each element of an array to a dataframe? - by deanhystad - Mar-29-2024, 10:32 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Is there a more elegant way to concatenate data frames? db042190 3 1,011 Jun-13-2023, 05:08 PM
Last Post: snippsat
  Change a numpy array to a dataframe Led_Zeppelin 3 1,172 Jan-26-2023, 09:01 PM
Last Post: deanhystad
  Seeing al the data in a dataframe or numpy.array Led_Zeppelin 1 1,196 Jul-11-2022, 08:54 PM
Last Post: Larz60+
  Apply fillna to multiple columns in dataframe rraillon 2 2,520 Aug-05-2021, 01:11 PM
Last Post: rraillon
  acess particular element in dataframe using .loc operator. shantanu97 0 1,454 Jun-30-2021, 03:59 AM
Last Post: shantanu97
  More elegant way to remove time from text lines. Pedroski55 6 4,040 Apr-25-2021, 03:18 PM
Last Post: perfringo
  IF statement to apply at each date illmattic 2 2,713 Apr-08-2021, 12:31 PM
Last Post: illmattic
  Compare each element of an array in a logic statement without using a for loop leocsmith 3 5,970 Apr-01-2021, 07:57 PM
Last Post: deanhystad
  How to apply a class method to an entire dataframe column tirtha9 1 5,205 Jan-03-2021, 04:44 AM
Last Post: klllmmm
  Choose an element from multidimensional array quest_ 2 2,692 Nov-25-2020, 12:59 AM
Last Post: quest_

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020