-1

I have a time column, with timestamps of the form 2018-04-12 06:48:39 . How can I add a column Month from this timestamp, in this case containing 4 ?

Qubix
  • 4,161
  • 7
  • 36
  • 73
  • 1
    Possible duplicate of [Spark DataFrame TimestampType - how to get Year, Month, Day values from field?](https://stackoverflow.com/questions/30949202/spark-dataframe-timestamptype-how-to-get-year-month-day-values-from-field) – pault Mar 08 '19 at 15:00

1 Answers1

2

pyspark.sql.functions.month:

import pyspark.sql.functions as F
df.withColumn('month', F.month('time')).show()
+-------------------+-----+
|               time|month|
+-------------------+-----+
|2018-04-12 06:48:39|    4|
+-------------------+-----+
Psidom
  • 209,562
  • 33
  • 339
  • 356