Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated currentdate.py to covert the string date type to spark date type #17

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions currentdate.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,26 @@
Created on Thu Oct 24 22:42:50 2019

@author: prabha

Modified on Wed Feb 21 2024
@author: Shahid

"""

import pyspark
from pyspark.sql import SparkSession
from pyspark.sql.functions import col
from pyspark.sql.functions import to_timestamp, current_timestamp
from pyspark.sql.types import StructType, StructField, StringType, IntegerType, LongType
from pyspark.sql.types import DateType
from pyspark.sql.functions import to_date

spark = SparkSession.builder.appName('SparkByExamples.com').getOrCreate()

schema = StructType([
StructField("seq", StringType(), True)])
# Sample data with a string column representing dates
data = [("1/12/2023",), ("2/15/2023",), ("3/20/2023",)]
columns = ["date_str"]

dates = ['1']
df = spark.createDataFrame(data, columns)

df = spark.createDataFrame(list('1'), schema=schema)
# Convert the string column to a DateType
df = df.withColumn("date", to_date("date_str", "M/d/yyyy").cast(DateType()))

df.show()
df.show()