0

I've seen dozens of similar questions but I couldn't make a final effect like I want.

Data frame structure is filled with measurements taken nearly every minute (25k+ rows):

                                     some_val
...
2020-02-22 00:00:00                  1.1
2020-02-22 00:01:01                  1.4
2020-02-22 00:02:01                  1.5
...
2020-02-23 00:00:05                  1.7
2020-02-23 00:01:05                  1.6
2020-02-23 00:02:06                  1.6
...
2020-02-24 00:00:02                  1.4
2020-02-24 00:01:03                  1.8
2020-02-24 00:02:03                  1.3

I want to group that data frame by each minute of a day - so first group (time of a day 00:00) should consist of [1.1, 1.7, 1.4] and so on.

Then I want to see some trends by plotting it on 1 figure where:

  • X axis is time with step of 1 minute (00:00, 00:01, ..., 23:59)
  • Y axis are some_val - as many measurements per each X time as they are in each group

For now I don't want to count() or mean() anything. Just simple grouping, plotting everything on 1 figure and saving it to file.

As I read I should be able to use pandas groupby or resample along with matplotlib.pyplot but somehow I couldn't manage to do it.

Thanks in advance.

adek111
  • 1,026
  • 2
  • 14
  • 29
  • maybe duplicate of https://stackoverflow.com/questions/16266019/python-pandas-group-datetime-column-into-hour-and-minute-aggregations – anki Feb 24 '20 at 21:57
  • As it suggests some solutions for grouping (that I tried before), it doesn't solve my problem with plotting all the data on 1 figure. – adek111 Feb 25 '20 at 07:34

2 Answers2

1

Solution Code

import pandas as pd

data={'Date':['2020-02-22T00:00:00.000000000', '2020-02-22T00:01:00.000000000',
           '2020-02-22T00:02:00.000000000'],'some_val':[1.1, 1.4, 1.5]}
    df=pd.DataFrame.from_dict(data)

Plotting

import matplotlib .pyplot as plt
df['Date']=pd.to_datetime(df['Date'])#Converting Date to datetime 
df.set_index(df['Date'], inplace=True)#Setting Date as index
df['x']= df.index.minute #Extracting minutes
df.plot(x='x', y='some_val')
wwnde
  • 26,119
  • 6
  • 18
  • 32
  • Thanks for your answer but your solution plots all the data sorted by each minute in 1 hour period. My question was to plot all the data by each minute in 1 day period. – adek111 Feb 25 '20 at 07:36
0

If you need each minute in a day please try;

data={'Date':['2020-02-22T00:00:00.000000000', '2020-02-22T00:01:00.000000000',
       '2020-02-22T00:02:00.000000000','2020-02-23T00:00:00.000000000', '2020-02-23T00:01:00.000000000',
       '2020-02-23T00:02:00.000000000'],'some_val':[1.1, 1.4, 1.5,2.1, 2.4, 2.5]}
    df=pd.DataFrame.from_dict(data)
    df['Date']=pd.to_datetime(df['Date'])
   df.set_index(df['Date'], inplace=True)
   df['x']= df.index.minute
   for x, df2 in df.groupby(df.index.date):
    df2.plot(x='x', y='some_val')
    plt.title(x)
wwnde
  • 26,119
  • 6
  • 18
  • 32