Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to format month in full in pyspark?

what’s the best way to format a date, with the month in full, for example, I get the value 01-01-2022 and I need to return 01JAN2022, I tried the following code but it didn’t work:

.withColumn(‘dt_ori’,upper(date_format(col("dt_ori"),"ddMMMyyyy")))

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

This is working as expected , based on the various Date Time patterns available within Spark 3.x

‘MMM’: Short textual representation in the standard form. The month
pattern should be a part of a date pattern not just a stand-alone
month except locales where there is no difference between stand and
stand-alone forms like in English.

Spark Version

  • Spark Version – 3.1.2
  • Java Version – 1.8.0_292 (AdoptOpenJDK)
  • Scala Version – 2.12.10

Date Format

s = StringIO("""
ID,date_str
101,01-01-2022
""")

df = pd.read_csv(s,delimiter=',')

sparkDF = sql.createDataFrame(df)\
            .withColumn('date_parsed',F.to_date(F.col('date_str'), 'dd-MM-yyyy'))\
            .withColumn('date_formated',F.upper(F.date_format(F.col('date_parsed'), 'ddMMMyyyy')))\
            .drop('date_str')

sparkDF.show()

+---+-----------+-------------+
| ID|date_parsed|date_formated|
+---+-----------+-------------+
|101| 2022-01-01|    01Jan2022|
+---+-----------+-------------+
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading