Preparation
Before any data manipulation can occur, two (2) new libraries will require installation.
- The Pandas library enables access to/from a DataFrame.
- The NumPy library supports multi-dimensional arrays and matrices in addition to a collection of mathematical functions.
To install these libraries, navigate to an IDE terminal. At the command prompt ($
), execute the code below. For the terminal used in this example, the command prompt is a dollar sign ($
). Your terminal prompt may be different.
$ pip install pandas
Hit the <Enter>
key on the keyboard to start the installation process.
$ pip install numpy
Hit the <Enter>
key on the keyboard to start the installation process.
If the installations were successful, a message displays in the terminal indicating the same.
Feel free to view the PyCharm installation guide for the required libraries.
Add the following code to the top of each code snippet. This snippet will allow the code in this article to run error-free.
import pandas as pd import numpy as np
DataFrame between_time()
The between_time() method selects and retrieves values occurring between set times. The return value is a DataFrame/Series.
The syntax for this method is as follows:
DataFrame.between_time(start_time, end_time, include_start=True, include_end=True, axis=None)
Parameter | Description |
---|---|
start_time | This parameter must be a valid datetime.time or string. |
| This parameter must be a valid datetime.time or string. |
include_start | By default, this is True , meaning the results include the start time. |
include_end | By default, this is True , meaning the results include the end time. |
axis | If zero (0) or index is selected, apply to each column. Default is None . If one (1) is selected, apply to each row. |
For this example, the stock prices for Apple returns at six (6) hour intervals.
todays_date = date.today() stock_drange = pd.date_range(todays_date, periods=6, freq='6H') stock_df = pd.DataFrame({'AAPL': [10.34, 9.83, 10.39, 8.54, 9.97, 11.98]}, index=stock_drange) print(stock_df) result = stock_df.between_time('06:00', '12:00') print(result)
- Line [1] uses
today()
to retrieve the current date/time and saves it totodays_date
. - Line [2] uses
date_range()
to set the date, period(s), frequencies and saves them tostock_drange
. - Line [3] creates a DataFrame. Sets the index to
stock_drange
and saves it tostock_df
. - Line [4] outputs the result to the terminal.
- Line [5] retrieves the stock prices between the specified start and end times and saves to the
result
variable. - Line [6] outputs the result to the terminal.
Output
AAPL | |
2022-01-06 00:00:00 | 10.34 |
2022-01-06 06:00:00 | 9.83 |
2022-01-06 12:00:00 | 10.39 |
2022-01-06 18:00:00 | 8.54 |
2022-01-07 00:00:00 | 9.97 |
2022-01-07 06:00:00 | 11.98 |
AAPL | |
2022-01-06 06:00:00 | 9.83 |
2022-01-06 12:00:00 | 10.39 |
2022-01-07 06:00:00 | 11.98 |
More Pandas DataFrame Methods
Feel free to learn more about the previous and next pandas DataFrame methods (alphabetically) here:
Also, check out the full cheat sheet overview of all Pandas DataFrame methods.