logo
down
shadow

Pandas concat historical data using date minus some number of days


Pandas concat historical data using date minus some number of days

By : User1991
Date : January 12 2021, 08:33 AM
seems to work fine I have a data frame where one of the columns is Date (i.e. 5/29/2007) and then there is col A and col B and a few other columns. The date and the value of col A and col B uniquely identify the row. I want to add columns to the data frame which are the historical values of other columns (not A or B). For example I have some row. Call it row i and it has some value for col D. And I want to add a column to the data frame which corresponds to the value of col D but for a previous day. That is the value of col D where the row, call it row j, has the same values for col A and col B as row i but the date is the date of row i minus one day. How can I do this efficiently? , I would create a shifted copy, then merge with the original.
code :
# shifted copy
shiftInterval = pd.Timedelta('-1 days') # your X days interval
dfShifted = df.copy()
dfShifted['Date'] = dfShifted['Date'] - shiftInterval
dfShifted.drop(['col-C'], axis=1, inplace=True)

# merge, keeping only observations where -1 lag is present
df2 = pd.merge(df,
           dfShifted,
           on=['Date','col-A','col-B'],
           how = 'inner', # use 'left' to keep observations without lags
           suffixes=['','-1'])

df2
#        Date col-A col-B  col-C  col-D  col-D-1
#0 2007-05-30     A     B     10      1        1
#1 2007-05-30    AA    Bf      3      1      124
#2 2007-05-30    AV    Bf      1      8        4
from io import StringIO
import pandas as pd
from datetime import datetime

df = pd.read_csv(StringIO("""Date,col-A,col-B,col-C,col-D
5/29/2007,A,B,0,1
5/29/2007,AA,Bf,7,124
5/29/2007,AV,Bf,1,4
5/30/2007,A,B,10,1
5/30/2007,AA,Bf,3,1
5/30/2007,AV,Bf,1,8"""))

df['Date'] = df['Date'].apply(lambda x: datetime.strptime(x, '%m/%d/%Y'))


Share : facebook icon twitter icon
VB.Net 2005, how can I subtract a date from the (current date minus 7 days) and produce a number of days?

VB.Net 2005, how can I subtract a date from the (current date minus 7 days) and produce a number of days?


By : k r saini
Date : March 29 2020, 07:55 AM
With these it helps I have a date in the future e.g. 13/10/2008 I need to subtract the current date (today is the 28/09/2010) minus 7 days, so thats 21/09/2010 minus 13/10/2008, which would equal erm, 720 something ?
code :
Sub Main()
    Dim dt As DateTime = New DateTime(2008, 10, 13)
    ' be careful what you are subtracting from what
    ' the date you have is not in the future (year 2008)
    ' if the date is in the future: (dt.Subtract(DateTime.Now.AddDays(-7))).TotalDays
    ' or simply take the absolute value
    Dim days As Double = (DateTime.Now.AddDays(-7).Subtract(dt)).TotalDays
    Console.WriteLine(days)
End Sub
Android Date Picker - Actual Date minus X number of days

Android Date Picker - Actual Date minus X number of days


By : Sonia
Date : March 29 2020, 07:55 AM
To fix this issue I can get the current date with the following code to show in DatePicker dialog for example. , Try using:
code :
    c.add(Calendar.YEAR, -18);
Bash Shell Current Date Minus Number of Days

Bash Shell Current Date Minus Number of Days


By : Andreas Kitzing
Date : March 29 2020, 07:55 AM
Any of those help I am new to bash and shell but I am running a debian install and I am trying to make a script which can find a date in the past without having to install any additional packages. From tutorials I have got to this stage: , Try doing this :
code :
#!/bin/sh
#
# BACKUP DB TO S3
#

# VARIABLES
TYPE="DATABASE"
DAYS="30"

# GET CURRENT DATETIME
CURRENTDATE="$(date +%Y%m%d%H%M%S)"

# GENERATE PAST DATE FROM DAYS CONSTANT
OLDERDATE="$(date "+%Y%m%d%H%M%S" -d "$DAYS days ago")"

# CALL PYTHON SCRIPT WITH OLDERDATE ARGUMENT
python script.py "$OLDERDATE"
How to calculate if the difference from current date and another date is equal or minus of a number of days

How to calculate if the difference from current date and another date is equal or minus of a number of days


By : user3147456
Date : March 29 2020, 07:55 AM
will help you I use this query to calculate if the current date minus another date is minus or equal an amount of days. Unfortunately this query only works if the calculation of days is inside the same month. Let's take for example these datas: , This is the right code for my goal:
code :
SELECT * FROM condominio WHERE TIMESTAMPDIFF(DAY,NOW(), DATE_ADD(revisione, INTERVAL 1 YEAR)) <= 30;
Get a new Date based on today's date minus or plus a specific number of days in one line

Get a new Date based on today's date minus or plus a specific number of days in one line


By : user3480169
Date : March 29 2020, 07:55 AM
wish of those help Simple question: , Try this:
code :
console.log(new Date(new Date().setDate(new Date().getDate() - 90)));
Related Posts Related Posts :
  • How to calculate each single element of a numpy array based on conditions
  • How do I change the width of Jupyter notebook's cell's left part?
  • Measure distance between lat/lon coordinates and utm coordinates
  • Installing megam for NLTK on Windows
  • filter dataframe on each value of a samn column have a specific value of another column in Panda\Python
  • Threading with pubsub throwing AssertionError: 'callableObj is not callable' in wxPython
  • Get grouped data from 2 dataframes with condition
  • How can I import all of sklearns regressors
  • How to take all elements except the first k
  • Whats wrong with my iteration list of lists from csv
  • Tensorflow Estimator API save image summary in eval mode
  • How to Pack with PyQt - how to make QFrame/Layout adapt to content
  • How do I get certain Time Range in Python
  • python doubly linked list - insertAfter node
  • Open .h5 file in Python
  • Joining a directory name with a binary file name
  • python, sort list with two arguments in compare function
  • Is it possible to print from Python using non-ANSI colors?
  • CV2: Import Error in Python OpenCV
  • shadow
    Privacy Policy - Terms - Contact Us © voile276.org