任务详情
给定一各地 2016 年 1 月和 2 月各个时间点的温度表格,表格预览见页面下方。
数据表的第二列表示当前时间,数据表第一行第三列到第一行最后一列表示地点。
如:表格的第二行第三列 30.36 表示:Vancouver (温哥华)在 2016-01-01 0 这一时刻的温度是 30.36 度(华氏度)。
程序给定一个日期(date)和一个地点(place),要求返回该地在这个日期下的温差(摄氏度)。
注意:华氏度需要转换成摄氏度!
华氏度转摄氏度公式:摄氏度 = (华氏度 - 32) * 5 / 9
温差:某日最高温度 - 某日最低温度
任务要求
程序给定日期 date 和地点 place 的数据类型均是 str 类型;
程序返回结果的数据类型是 float 类型,如果最终结果是 numpy.float64 数据类型,请手动转换成 Python 原生的 float 数据类型;
结果需要四舍五入保留小数点后 2 位,请在得到最终结果后处理小数问题,否则可能导致结果有偏差。
测试用例
任务实现
import pandas class Solution: ? ? def temperature(self, date: str, place: str) -> float: ? ? ? ? # 将查询日期转换为日期字符串 ? ? ? ? month, day_ = date.split("月") ? ? ? ? day, _ = day_.split("日") ? ? ? ? if len(month) == 1: ? ? ? ? ? ? month = "0" + month ? ? ? ? if len(day) == 1: ? ? ? ? ? ? day = "0" + day ? ? ? ? date_str = "2016" + "-" + month + "-" + day ? ? ? ? # 读取数据 ? ? ? ? url = "http://ws1.itmc.org.cn:80/JS00101/data/user/4799/208/fj_4097_temperature_2016_1_2.csv" ? ? ? ? temperature_data = pandas.read_csv(url, sep=",") ? ? ? ? # 获取数据的日期字符串 ? ? ? ? temperature_data["day"] = temperature_data["datetime"].str[:10] ? ? ? ? # 根据日期字符串选择指定日期数据行 ? ? ? ? day = temperature_data[temperature_data["day"] == date_str] ? ? ? ? # 根据地点选择选择温度数据行 ? ? ? ? city = day[place] ? ? ? ? # 温度单位转换 ? ? ? ? temperature = (city.max() - city.min() - 32) * 5 / 9 ? ? ? ? # 温度精度处理 ? ? ? ? return float(temperature.round(2)) print(Solution.temperature(Solution, date="2月5日", place="Dallas")) print(Solution.temperature(Solution, date="2月18日", place="Houston")) print(Solution.temperature(Solution, date="1月21日", place="Las Vegas"))
输出为:
到此这篇关于pandas温差查询案例的实现的文章就介绍到这了,更多相关pandas温差查询 内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://haodehen.cn/did16512