Pandas - Time Functions
Time Function
Pandas is one of the important library you can use to manage your data. One of the functions that Pandas can do is to deal with time matters. On this page, we will talk about date_range, to_datetime, shift functions of Pandas.
1. date_range:
date_range is like np.arange in terms of time. For example, np.arange(5) =np.array( [0,1,2,3,4]). It
gives you an array of integers in a range of 5. Similarly we can do this using pandas.date_range.
First, you want to choose exactly three of the parameters: start, end, periods, and freq. Start is the
beginning of the time period and the end is the end of the time period. Periods is the number of data
you want in return. Lastly, freq is an input that determines the gaps between data values. For instance,
we want to know all Saturdays before 2023 Christmas from now on and this is what I typed:
Since we start today, October 16th, we put it as a start input and end = the Christmas. I put W-SAT,
Every Saturday, as a frequent input.
Now, here is the request we want to solve.
Since every paycheck starts every other Friday, we will put March 14th instead of March 13th. With 93
paychecks, we will put that in the period part. As I said, it's every other Friday, so we will put 2W-FRI
to make it every other Friday. Then this is what we get:
2. to_datetime:
When we have a list of dates but it's not in the exact form that pandas can accept, we can use
pandas.to_datetime(). Let's say we have 4 different October 1sts in 4 years. Since it's not in the exact
form, we will put %m, %d, %Y for month, date, and year respectively. Then we can get the correct
form of date time index.
We have a data called, 'DJIA.csv' which contains daily closing values of the Dow Jones Industrial
average from 2006 to 2016. Since we can see the date index, we will change that into a date time index.
All we have to do is this: data.index = pd.to_datetime(data.index)
3. shift:
Sometimes, you have a moment that you have written something one shift down. You can solve that
issue very easily on Pandas. Let's see with an example:
We have the same 'DJIA.csv' after some modifications. If we do shift(1):
On the other hand, if we do shift(-1):
daily. To do that we just have to data - data(1):
Then you can some of the dates that had loses and dates that had more money than each of the
previous days.
*GitHub: https://github.com/KwakSukyoung/coding/blob/master/ACME/Pandas1/pandas1.ipynb











Comments
Post a Comment