I have a very simple CSV, call it test.csv
name,timestamp,action
A,2012-10-12 00:30:00.0000000,1
B,2012-10-12 01:00:00.0000000,2
C,2012-10-12 01:30:00.0000000,2
D,2012-10-12 02:00:00.0000000,3
E,2012-10-12 02:30:00.0000000,1
I'm trying to read it using pyspark and add a new column indicating the month.
First I read in the data, and everything looks ok.
df = spark.read.csv('test.csv', inferSchema=True, header=True)
df.printSchema()
df.show()
Output:
root
|-- name: string (nullable = true)
|-- timestamp: timestamp (nullable = true)
|-- action: double (nullable = true)
+----+-------------------+------+
|name| timestamp|action|
+----+-------------------+------+
| A|2012-10-12 00:30:00| 1.0|
| B|2012-10-12 01:00:00| 2.0|
| C|2012-10-12 01:30:00| 2.0|
| D|2012-10-12 02:00:00| 3.0|
| E|2012-10-12 02:30:00| 1.0|
+----+-------------------+------+
But when I try to add my column, the formatting option doesn't seem to do anything.
df.withColumn('month', to_date(col('timestamp'), format='MMM')).show()
Output:
+----+-------------------+------+----------+
|name| timestamp|action| month|
+----+-------------------+------+----------+
| A|2012-10-12 00:30:00| 1.0|2012-10-12|
| B|2012-10-12 01:00:00| 2.0|2012-10-12|
| C|2012-10-12 01:30:00| 2.0|2012-10-12|
| D|2012-10-12 02:00:00| 3.0|2012-10-12|
| E|2012-10-12 02:30:00| 1.0|2012-10-12|
+----+-------------------+------+----------+
What's going on here?