这个问题与Resolving ValidationError: [u"'' value has an invalid date format. It must be in YYYY-MM-DD format."] in Django 1.9.2?类似,但我看不出该怎么做。我正在使用django 2.0.4并且他们使用的是1.9.2
我有以下models.py
from django.db import models
import datetime
class Flight(models.Model):
date = models.DateField(default=datetime.date.today)
dep_time = models.TimeField()
dep_delay = models.CharField(max_length=100)
arr_time = models.TimeField()
arr_delay = models.DurationField()
cancelled = models.BooleanField()
carrier = models.CharField(max_length=100)
tailnum = models.IntegerField()
flight = models.CharField(max_length=100)
origin = models.CharField(max_length=100)
dest = models.CharField(max_length=100)
air_time = models.CharField(max_length=100)
distance = models.CharField(max_length=100)
duration = models.DurationField()
def __str__(self):
return f'{self.flight} {self.dest} {self.date}'
我正在尝试从django shell运行此脚本来更新我的数据库:
data = data.applymap(str)
data['month'] = data['month'].str.pad(2,side='left',fillchar='0')
data['day'] = data['day'].str.pad(2,side='left',fillchar='0')
data['date'] = data[['year', 'month', 'day']].apply(lambda x: ''.join(x), axis=1)
data['date'] = pd.to_datetime(data['date'],infer_datetime_format=True)
data['dep_time'] = pd.to_datetime(data['dep_time'].str.pad(4,side='left',fillchar='0'),format='%H%M',errors='coerce').dt.time
data['arr_time'] = pd.to_datetime(data['arr_time'].str.pad(4,side='left',fillchar='0'),format='%H%M',errors='coerce').dt.time
data['duration'] = data[['hour', 'min']].apply(lambda x: ''.join(x), axis=1)
data['duration'] = pd.to_datetime(data['duration'].str.pad(4,side='left',fillchar='0'),format='%H%M',errors='coerce').dt.time
data['date'] = data[['month', 'day', 'year']].apply(lambda x: '/'.join(x), axis=1)
data = data.drop(['year', 'month', 'day', 'hour', 'min'], axis=1)
cols = ['date', 'dep_time', 'dep_delay', 'arr_time',
'arr_delay', 'cancelled', 'carrier',
'tailnum', 'flight', 'origin',
'dest', 'air_time', 'distance', 'duration']
data = data.reindex(columns=cols)
print(data.tail())
#
def timeStrFormat(time):
time = str(time)
newTime = time[:-2] + ':' + time[-2:]
return newTime
flights = [
Flight(
date = data.iloc[row].loc['date'],
dep_time = data.iloc[row].loc['dep_time'],
dep_delay = data.iloc[row].loc['dep_delay'],
arr_time = data.iloc[row].loc['arr_time'],
arr_delay = data.iloc[row].loc['arr_delay'],
cancelled = data.iloc[row].loc['cancelled'],
carrier = data.iloc[row].loc['carrier'],
tailnum = data.iloc[row].loc['tailnum'],
flight = data.iloc[row].loc['flight'],
origin = data.iloc[row].loc['origin'],
dest = data.iloc[row].loc['dest'],
air_time = data.iloc[row].loc['air_time'],
distance = data.iloc[row].loc['distance'],
duration = data.iloc[row].loc['duration'],
)
for row, _ in enumerate(data)
]
for f in flights: f.save()
我收到以下stacktrace错误:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "<string>", line 56, in <module>
File "/Users/davidthompson/miniconda3/lib/python3.6/site-packages/django/db/models/base.py", line 729, in save
force_update=force_update, update_fields=update_fields)
File "/Users/davidthompson/miniconda3/lib/python3.6/site-packages/django/db/models/base.py", line 759, in save_base
updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/Users/davidthompson/miniconda3/lib/python3.6/site-packages/django/db/models/base.py", line 842, in _save_table
result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "/Users/davidthompson/miniconda3/lib/python3.6/site-packages/django/db/models/base.py", line 880, in _do_insert
using=using, raw=raw)
File "/Users/davidthompson/miniconda3/lib/python3.6/site-packages/django/db/models/manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/Users/davidthompson/miniconda3/lib/python3.6/site-packages/django/db/models/query.py", line 1125, in _insert
return query.get_compiler(using=using).execute_sql(return_id)
File "/Users/davidthompson/miniconda3/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1283, in execute_sql
for sql, params in self.as_sql():
File "/Users/davidthompson/miniconda3/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1236, in as_sql
for obj in self.query.objs
File "/Users/davidthompson/miniconda3/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1236, in <listcomp>
for obj in self.query.objs
File "/Users/davidthompson/miniconda3/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1235, in <listcomp>
[self.prepare_value(field, self.pre_save_val(field, obj)) for field in fields]
File "/Users/davidthompson/miniconda3/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1175, in prepare_value
value = field.get_db_prep_save(value, connection=self.connection)
File "/Users/davidthompson/miniconda3/lib/python3.6/site-packages/django/db/models/fields/__init__.py", line 767, in get_db_prep_save
return self.get_db_prep_value(value, connection=connection, prepared=False)
File "/Users/davidthompson/miniconda3/lib/python3.6/site-packages/django/db/models/fields/__init__.py", line 1276, in get_db_prep_value
value = self.get_prep_value(value)
File "/Users/davidthompson/miniconda3/lib/python3.6/site-packages/django/db/models/fields/__init__.py", line 1271, in get_prep_value
return self.to_python(value)
File "/Users/davidthompson/miniconda3/lib/python3.6/site-packages/django/db/models/fields/__init__.py", line 1246, in to_python
params={'value': value},
django.core.exceptions.ValidationError: ["'01/01/2014' value has an invalid date format. It must be in YYYY-MM-DD format."]
我的df看起来像这样:
date dep_time dep_delay arr_time arr_delay cancelled carrier \
103992 05/11/2014 10:13:00 -2 12:47:00 -13 0 B6
103993 05/11/2014 19:29:00 -1 22:15:00 -24 0 B6
103994 05/11/2014 11:17:00 5 13:55:00 9 0 B6
103995 05/11/2014 07:36:00 -10 09:24:00 -18 0 B6
103996 05/11/2014 13:40:00 0 16:47:00 10 0 B6
我是否需要强制pandas以YYYY-MM-DD格式呈现日期?我没有看到如何做到这一点,而不将其转换为str,我想把它作为日期对象放入sqlite3
答案 0 :(得分:1)
将DF中的日期转换为datetime对象,然后将其插入模型中。
<强>实施例强>
import pandas as pd
df = pd.DataFrame({"Date": ["05/11/2014", "05/11/2014"]})
df["Date"] = pd.to_datetime(df["Date"])
print(df)
<强>输出:强>
Date
0 2014-05-11
1 2014-05-11