cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
cancel
Showing results for 
Search instead for 
Did you mean: 

Date field getting changed when reading from excel file to dataframe in pyspark

Pradeep_Namani
New Contributor III

The date field is getting changed while reading data from source .xls file to the dataframe. In the source xl file all columns are strings but i am not sure why date column alone behaves differently

In Source file date is 1/24/1947.

In pyspark dataframe it is 1/24/47

Code used:

df=spark.read.format("com.crealytics.spark.excel").option("header","true").load("/mnt/dataplatform/Tenant_PK/Results.xlsx")

If I use option("inforscheme","true") the data coming properly , but I dont want use inforschema, Can any one suggest me any solution.

Thanks in advance

5 REPLIES 5

yogu
Honored Contributor III

hi  @Pradeep Namani​ ,

could you plz try to run below one. I hope so it will work without inferschema

df=spark.read.format("csv").option("header","true").load("/mnt/dataplatform/Tenant_PK/Results.xlsx")

Pradeep_Namani
New Contributor III

Thank you @Yogita Chavan​  for replying , but when I am reading file as csv it is showing all data in different format, I am attaching the screen shot

ADB issue

yogu
Honored Contributor III

Pradeep_Namani
New Contributor III

ADB issue1I have tried the option which we have give in above url but no use, still I am facing same issue

-werners-
Esteemed Contributor III

how about using inferschema one single time to create a correct DF, then create a schema from the df-schema.

something like this f.e.

from pyspark.sql.types import StructType    
 
# Save schema from the original DataFrame into json:
schema_json = df.schema.json()
 
# Restore schema from json:
import json
new_schema = StructType.fromJson(json.loads(schema_json))

Welcome to Databricks Community: Lets learn, network and celebrate together

Join our fast-growing data practitioner and expert community of 80K+ members, ready to discover, help and collaborate together while making meaningful connections. 

Click here to register and join today! 

Engage in exciting technical discussions, join a group with your peers and meet our Featured Members.