【发布时间】:2020-03-23 06:32:56
【问题描述】:
我有一个 .csv 表,如下所示
将熊猫导入为 pd df = pd.read_csv('myFile.csv') df.head(3)
identifier identifier_type timestamp device_lat device_lon
0 68d62a1b-b928-4225-b445-9607415905b3 gaid 2020-03-19 03:03:00 UTC 44.808169 -73.522956
1 1675a629-a010-44b6-98a9-72d04793821f gaid 2020-03-18 21:15:42 UTC 42.103894 -76.799164
2 0fe7a0b7-028e-459e-b5d8-b59d31800b8e gaid 2020-03-18 23:39:54 UTC 43.182028 -77.672017
我正在用pyspark阅读它
schema= StructType([
StructField("identifier", StringType(), True),
StructField("identifier_type", StringType(), True),
StructField("timestamp", DateType(), True),
StructField("device_lat", FloatType(), True),
StructField("device_lon", FloatType(), True)])
myTable = spark.read.format("csv").schema(schema).load('NY_data/f0.csv')
myTable = myTable[myTable['device_lat']>0]
myTable.show(3)
+--------------------+---------------+----------+----------+----------+
| identifier|identifier_type| timestamp|device_lat|device_lon|
+--------------------+---------------+----------+----------+----------+
|68d62a1b-b928-422...| gaid|2020-03-19| 44.80817| -73.52296|
|1675a629-a010-44b...| gaid|2020-03-18| 42.103893|-76.799164|
|0fe7a0b7-028e-459...| gaid|2020-03-18| 43.18203| -77.67202|
+--------------------+---------------+----------+----------+----------+
为什么分、时、秒的信息消失了?
如果我尝试输入TimestampType 而不是DateType
schema= StructType([
StructField("identifier", StringType(), True),
StructField("identifier_type", StringType(), True),
StructField("timestamp", TimestampType(), True),
StructField("device_lat", FloatType(), True),
StructField("device_lon", FloatType(), True)])
myTable = spark.read.format("csv").schema(schema).load('NY_data/f0.csv')
myTable = myTable[myTable['device_lat']>0]
sqlContext.registerDataFrameAsTable(myTable, "myTable")
这就是我得到的
myTable.show(3)
+----------+---------------+---------+----------+----------+
|identifier|identifier_type|timestamp|device_lat|device_lon|
+----------+---------------+---------+----------+----------+
+----------+---------------+---------+----------+----------+
变量的类型是。
df.dtypes
identifier object
identifier_type object
timestamp object
device_lat float64
device_lon float64
dtype: object
【问题讨论】:
-
@AlexW 我试过了,但我得到了空值,正如您在修改后的问题中看到的那样
-
df.dtypes长什么样子? -
@AlexW 我添加了信息
-
你可以试试
df['timestamp'] = pd.to_datetime(df['timestamp'].copy(), infer_datetime_format=True)看看它是否会产生更好的结果? -
@AlexW 现在它给出了
timestamp datetime64[ns, UTC]但我想直接在 pyspark 中阅读它。