0
votes

My data frame looks like

    id      |reg_date  |      txn_date|
+----------+----------+--------------------+
|1          |2019-01-06| 2019-02-15 12:51:15|
|1          |2019-01-06| 2019-03-29 13:15:27|
|1          |2019-01-06| 2019-06-01 01:42:57|
|1          |2019-01-06| 2019-01-06 17:01:...|
|5          |2019-06-16| 2019-07-19 11:50:34|
|5          |2019-06-16| 2019-07-13 19:49:39|
|5          |2019-06-16| 2019-08-27 17:37:22|
|2          |2018-07-30| 2019-01-01 07:03:...|
|2          |2018-07-30| 2019-07-30 01:27:57|
|2          |2018-07-30| 2019-02-01 00:08:35

I want to pickup the 1st txn_date after reg_date , i.e. the first txn_date of reg_date >= txn_date.

Expected output

    id      |reg_date  |      txn_date|
+----------+----------+--------------------+
|1          |2019-01-06| 2019-01-06 17:01:...|
|5          |2019-06-16| 2019-07-13 19:49:39|
|2          |2018-07-30| 2019-07-30 01:27:57|

I have done so far,

df = df.withColumn('txn_date',to_date(unix_timestamp(F.col('txn_date'),'yyyy-MM-dd HH:mm:ss').cast("timestamp")))

df = df.withColumn('reg_date',to_date(unix_timestamp(F.col('reg_date'),'yyyy-MM-dd').cast("timestamp")))

gg = df.groupBy('id','reg_date').agg(min(F.col('txn_date')))

But getting wrong results.

2
It would be nice if you have prepared the data to create dataframe. What is the output you are getting. ? - PIG
@PIG - there is 2 conditions..one is reg_date>= txn_date another is based on this filter using groupby operation find min.txn_date after reg_date. In my case I got some results which satisfy reg_date< txn_date - John Davis
@John Davis Where is column 'mobile' in your df? - QuantStats
@QuantStats - it's not mobile it's id - John Davis

2 Answers

0
votes

The condition reg_date >= txn_date can be ambiguous.

Does 2019-01-06>=2019-01-06 17:01:30 mean 2019-01-06 00:00:00>=2019-01-06 17:01:30 or 2019-01-06 23:59:59>=2019-01-06 17:01:30?

In your example, 2019-01-06>=2019-01-06 17:01:30 is evaluated to be true, so I assume it is the latter case, i.e. the case with 23:59:59.

Proceeding with the assumption above, here is how I coded it.

import pyspark.sql.functions as F

#create a sample data frame
data = [('2019-01-06','2019-02-15 12:51:15'),('2019-01-06','2019-03-29 13:15:27'),('2019-01-06','2019-01-06 17:01:30'),\
('2019-07-30','2019-07-30 07:03:01'),('2019-07-30','2019-07-30 01:27:57'),('2019-07-30','2019-07-30 00:08:35')]

cols = ('reg_date', 'txn_date')

df = spark.DataFrame(data,cols)

#add 23:59:59 to reg_date as a dummy_date for a timestamp comparison later
df = df.withColumn('dummy_date', F.concat(F.col('reg_date'), F.lit(' 23:59:59')))

#convert columns to the appropriate time data types
df = df.select([F.to_date(F.col('reg_date'),'yyyy-MM-dd').alias('reg_date'),\
F.to_timestamp(F.col('txn_date'),'yyyy-MM-dd HH:mm:ss').alias('txn_date'),\
F.to_timestamp(F.col('dummy_date'),'yyyy-MM-dd HH:mm:ss').alias('dummy_date')])

#implementation part
(df.orderBy('reg_date')
   .filter(F.col('dummy_date')>=F.col('txn_date'))
   .groupBy('reg_date')
   .agg(F.first('txn_date').alias('txn_date'))
   .show()) 

#+----------+----------------------+
#|  reg_date|              txn_date|
#+----------+----------------------+
#|2019-01-06|   2019-01-06 17:01:30|
#|2019-07-30|   2019-07-30 07:03:01|
#+----------+----------------------+

0
votes

You don't need to order. You can discard all smaller values with a filter, then aggregate by id and get the smaller timestamp, because the first timestamp will be the minimum. Something like:

df.filter(df.reg_date >= df.txn_date) \
  .groupBy(df.reg_date) \
  .agg(F.min(df.txn_date)) \
  .show()