Before any data manipulation can occur, one (1) new library will require installation.
- The Pandas library enables access to/from a DataFrame.
To install this library, 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.
💡 Note: The
pytz comes packaged with pandas and does not require installation. However, this library is needed for the
tz_ localize() and
tz_convert() methods to work.
$ pip install pandas
<Enter> key on the keyboard to start the installation process.
If the installation was successful, a message displays in the terminal indicating the same.
Feel free to view the PyCharm installation guide for the required library.
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 pytz
to_timestamp() method casts (converts) data to a
Datetimeindex of timestamps at the start of a selected period.
The syntax for this method is as follows:
DataFrame.to_timestamp(freq=None, how='start', axis=0, copy=True)
|This parameter is an available frequency of the |
|This parameter is the period conversion to timestamp. The available options are: |
|If zero (0) or index is selected, apply to each column. Default 0.|
If one (1) apply to each row.
For this example, we have four quarter earnings for Rivers Clothing for 2021. Each row displays a quarter-end date and total earning amount for that time.
earnings = [120545, 230574, 101155, 17598] the_range = pd.period_range('2021Q1', '2021Q4', freq='Q-DEC') times = pd.Series(earnings, the_range) times.index = (the_range.asfreq('M', 'e')).asfreq('H', 's')+8 print(times)
- Line  saves the quarterly earnings for Rivers Clothing in 2021 to a list.
- Line  sets the date range (quarterly) and frequency. This output saves to the_range.
- Line  sets the index and asfreq() month and hour. The start hour for each quarter is 8:00 am.
- Line  outputs the times variable to the terminal.
|Freq: H, dtype: int64|
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.
At university, I found my love of writing and coding. Both of which I was able to use in my career.
During the past 15 years, I have held a number of positions such as:
In-house Corporate Technical Writer for various software programs such as Navision and Microsoft CRM
Corporate Trainer (staff of 30+)
Implementation Specialist for Navision and Microsoft CRM
Senior PHP Coder