add_dates
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
add_dates [2020/12/03 19:02] – [without pandas; single date] raju | add_dates [2021/01/13 14:41] (current) – raju | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | === Add dates === | + | ===== Add dates ===== |
+ | |||
+ | ==== single start date; multiple offsets; with pandas ==== | ||
+ | prepare the input | ||
+ | < | ||
+ | In [1]: | ||
+ | start_yyyymmdd = ' | ||
+ | fmt = ' | ||
+ | from datetime import datetime | ||
+ | start_date = datetime.strptime(start_yyyymmdd, | ||
+ | |||
+ | In [2]: | ||
+ | print(start_date) | ||
+ | 2020-12-03 00:00:00 | ||
+ | |||
+ | In [3]: | ||
+ | import pandas as pd | ||
+ | days_original = [158, 928, 882, 341, 596, 878, 526] | ||
+ | offset_years = [round(x/ | ||
+ | df = pd.DataFrame({' | ||
+ | |||
+ | In [4]: | ||
+ | print(df) | ||
+ | | ||
+ | 0 0.432877 | ||
+ | 1 2.542466 | ||
+ | 2 2.416438 | ||
+ | 3 0.934247 | ||
+ | 4 1.632877 | ||
+ | 5 2.405479 | ||
+ | 6 1.441096 | ||
+ | </ | ||
+ | Convert the offset to days | ||
+ | < | ||
+ | In [5]: | ||
+ | import numpy as np | ||
+ | df[' | ||
+ | df | ||
+ | Out[5]: | ||
+ | | ||
+ | 0 0.432877 | ||
+ | 1 2.542466 | ||
+ | 2 2.416438 | ||
+ | 3 0.934247 | ||
+ | 4 1.632877 | ||
+ | 5 2.405479 | ||
+ | 6 1.441096 | ||
+ | </ | ||
+ | Apply the offset to get end dates | ||
+ | < | ||
+ | In [6]: | ||
+ | df[' | ||
+ | df | ||
+ | Out[6]: | ||
+ | | ||
+ | 0 0.432877 | ||
+ | 1 2.542466 | ||
+ | 2 2.416438 | ||
+ | 3 0.934247 | ||
+ | 4 1.632877 | ||
+ | 5 2.405479 | ||
+ | 6 1.441096 | ||
+ | </ | ||
+ | |||
+ | Used | Python 3.8.5, ipython 7.18.1, pandas 1.1.3, numpy 1.19.2 | ||
+ | |||
+ | demonstrates | round float to int | ||
==== multiple dates; different offsets; with pandas ==== | ==== multiple dates; different offsets; with pandas ==== | ||
Given | Given | ||
Line 68: | Line 135: | ||
==== single date; single offset; without pandas ==== | ==== single date; single offset; without pandas ==== | ||
- | For a single date, we can do this without pandas also | ||
< | < | ||
In [1]: | In [1]: | ||
Line 82: | Line 148: | ||
==== Related links ==== | ==== Related links ==== | ||
* https:// | * https:// | ||
+ | * https:// | ||
==== offset calculation ==== | ==== offset calculation ==== | ||
Line 87: | Line 154: | ||
For example, let's say: | For example, let's say: | ||
- | * There are two applications App1 and App2 where App1 provides input to App2 | + | * There are two applications App1 and App2 |
- | * App1 converts the number of days (an integer value) into number of years (a floating point number) and rounds it to 9 digits | + | * App1 converts the number of days (an integer value) into number of years (a floating point number) and rounds it to 9 digits |
+ | * App2 reads that as input | ||
< | < | ||
Line 114: | Line 182: | ||
</ | </ | ||
- | ==== single start date; multiple offsets; with pandas ==== | ||
- | prepare the input | ||
- | < | ||
- | In [1]: | ||
- | start_yyyymmdd = ' | ||
- | fmt = ' | ||
- | from datetime import datetime | ||
- | start_date = datetime.strptime(start_yyyymmdd, | ||
- | |||
- | In [2]: | ||
- | print(start_date) | ||
- | 2020-12-03 00:00:00 | ||
- | |||
- | In [3]: | ||
- | import pandas as pd | ||
- | days_original = [158, 928, 882, 341, 596, 878, 526] | ||
- | offset_years = [round(x/ | ||
- | df = pd.DataFrame({' | ||
- | |||
- | In [4]: | ||
- | print(df) | ||
- | | ||
- | 0 0.432877 | ||
- | 1 2.542466 | ||
- | 2 2.416438 | ||
- | 3 0.934247 | ||
- | 4 1.632877 | ||
- | 5 2.405479 | ||
- | 6 1.441096 | ||
- | </ | ||
- | Convert the offset to days | ||
- | < | ||
- | In [5]: | ||
- | import numpy as np | ||
- | df[' | ||
- | df | ||
- | Out[5]: | ||
- | | ||
- | 0 0.432877 | ||
- | 1 2.542466 | ||
- | 2 2.416438 | ||
- | 3 0.934247 | ||
- | 4 1.632877 | ||
- | 5 2.405479 | ||
- | 6 1.441096 | ||
- | </ | ||
- | Apply the offset to get end dates | ||
- | < | ||
- | In [6]: | ||
- | df[' | ||
- | df | ||
- | Out[6]: | ||
- | | ||
- | 0 0.432877 | ||
- | 1 2.542466 | ||
- | 2 2.416438 | ||
- | 3 0.934247 | ||
- | 4 1.632877 | ||
- | 5 2.405479 | ||
- | 6 1.441096 | ||
- | </ | ||
- | |||
- | I used Python 3.8.5, ipython 7.18.1, pandas 1.1.3, numpy 1.19.2 |
add_dates.1607022160.txt.gz · Last modified: 2020/12/03 19:02 by raju