Converting time stamp to to_date in pandas
I am building and app for sql query genreation , i am able to generate it but receiving output in timestamp format , please help in converting it to to_date format.
Here is my code:-
from pandas import *
table_name="ADI"
file_name=pandas.read_excel('supermarke.xlsx')
def SQL_Insert(SOURCE, TARGET):
sql_texts =
for index, row in SOURCE.iterrows():
sql_texts.append(
'INSERT INTO ' + TARGET + ' (' + str(', '.join(SOURCE.columns)) + ') VALUES ' + str(tuple(row.values))+";")
return ('n'.join(sql_texts))
print(SQL_Insert(file_name, table_name))
Here is the output i am getting :-
INSERT INTO ADI (ID, Address, City, State, Country, Supermarket Name, Number of Employees, DATE) VALUES (1, '3666 21st St', 'San Francisco', 'CA 94114', 'USA', 'Madeira', 8, Timestamp('2018-01-12 00:00:00'));
INSERT INTO ADI (ID, Address, City, State, Country, Supermarket Name, Number of Employees, DATE) VALUES (2, '735 Dolores St', 'San Francisco', 'CA 94119', 'USA', 'Bready Shop', 15, Timestamp('2018-01-12 00:00:00'));
INSERT INTO ADI (ID, Address, City, State, Country, Supermarket Name, Number of Employees, DATE) VALUES (3, '332 Hill St', 'San Francisco', 'California 94114', 'USA', 'Super River', 25, Timestamp('2018-01-12 00:00:00'));
INSERT INTO ADI (ID, Address, City, State, Country, Supermarket Name, Number of Employees, DATE) VALUES (4, '3995 23rd St', 'San Francisco', 'CA 94114', 'USA', "Ben's Shop", 10, Timestamp('2018-01-12 00:00:00'));
Here what i am expecting :-
INSERT INTO ADI (ID, Address, City, State, Country, Supermarket Name, Number of Employees, DATE) VALUES (1, '3666 21st St', 'San Francisco', 'CA 94114', 'USA', 'Madeira', 8, TO_DATE('12/01/2018 00:00:00', 'MM/DD/YYYY HH24:MI:SS');
INSERT INTO ADI (ID, Address, City, State, Country, Supermarket Name, Number of Employees, DATE) VALUES (2, '735 Dolores St', 'San Francisco', 'CA 94119', 'USA', 'Bready Shop', 15, TO_DATE('12/01/2018 00:00:00', 'MM/DD/YYYY HH24:MI:SS');
INSERT INTO ADI (ID, Address, City, State, Country, Supermarket Name, Number of Employees, DATE) VALUES (3, '332 Hill St', 'San Francisco', 'California 94114', 'USA', 'Super River', 25,TO_DATE('12/01/2018 00:00:00', 'MM/DD/YYYY HH24:MI:SS');
INSERT INTO ADI (ID, Address, City, State, Country, Supermarket Name, Number of Employees, DATE) VALUES (4, '3995 23rd St', 'San Francisco', 'CA 94114', 'USA', "Ben's Shop", 10, TO_DATE('12/01/2018 00:00:00', 'MM/DD/YYYY HH24:MI:SS');
Please help me out here
pandas
add a comment |
I am building and app for sql query genreation , i am able to generate it but receiving output in timestamp format , please help in converting it to to_date format.
Here is my code:-
from pandas import *
table_name="ADI"
file_name=pandas.read_excel('supermarke.xlsx')
def SQL_Insert(SOURCE, TARGET):
sql_texts =
for index, row in SOURCE.iterrows():
sql_texts.append(
'INSERT INTO ' + TARGET + ' (' + str(', '.join(SOURCE.columns)) + ') VALUES ' + str(tuple(row.values))+";")
return ('n'.join(sql_texts))
print(SQL_Insert(file_name, table_name))
Here is the output i am getting :-
INSERT INTO ADI (ID, Address, City, State, Country, Supermarket Name, Number of Employees, DATE) VALUES (1, '3666 21st St', 'San Francisco', 'CA 94114', 'USA', 'Madeira', 8, Timestamp('2018-01-12 00:00:00'));
INSERT INTO ADI (ID, Address, City, State, Country, Supermarket Name, Number of Employees, DATE) VALUES (2, '735 Dolores St', 'San Francisco', 'CA 94119', 'USA', 'Bready Shop', 15, Timestamp('2018-01-12 00:00:00'));
INSERT INTO ADI (ID, Address, City, State, Country, Supermarket Name, Number of Employees, DATE) VALUES (3, '332 Hill St', 'San Francisco', 'California 94114', 'USA', 'Super River', 25, Timestamp('2018-01-12 00:00:00'));
INSERT INTO ADI (ID, Address, City, State, Country, Supermarket Name, Number of Employees, DATE) VALUES (4, '3995 23rd St', 'San Francisco', 'CA 94114', 'USA', "Ben's Shop", 10, Timestamp('2018-01-12 00:00:00'));
Here what i am expecting :-
INSERT INTO ADI (ID, Address, City, State, Country, Supermarket Name, Number of Employees, DATE) VALUES (1, '3666 21st St', 'San Francisco', 'CA 94114', 'USA', 'Madeira', 8, TO_DATE('12/01/2018 00:00:00', 'MM/DD/YYYY HH24:MI:SS');
INSERT INTO ADI (ID, Address, City, State, Country, Supermarket Name, Number of Employees, DATE) VALUES (2, '735 Dolores St', 'San Francisco', 'CA 94119', 'USA', 'Bready Shop', 15, TO_DATE('12/01/2018 00:00:00', 'MM/DD/YYYY HH24:MI:SS');
INSERT INTO ADI (ID, Address, City, State, Country, Supermarket Name, Number of Employees, DATE) VALUES (3, '332 Hill St', 'San Francisco', 'California 94114', 'USA', 'Super River', 25,TO_DATE('12/01/2018 00:00:00', 'MM/DD/YYYY HH24:MI:SS');
INSERT INTO ADI (ID, Address, City, State, Country, Supermarket Name, Number of Employees, DATE) VALUES (4, '3995 23rd St', 'San Francisco', 'CA 94114', 'USA', "Ben's Shop", 10, TO_DATE('12/01/2018 00:00:00', 'MM/DD/YYYY HH24:MI:SS');
Please help me out here
pandas
Quick fix: in your loop, first build the date string usingstrftime, then include it into the string withstr(tuple(row.values[:-1])) + custom_date_str + ";". Bigger fix: I suggest not manually building SQL query strings, and using a library like PyPika.
– Peter Leimbigler
Nov 24 '18 at 16:42
@PeterLeimbigler, please elaborate , i didn't get it .Thanks
– aditya singh
Nov 24 '18 at 16:50
add a comment |
I am building and app for sql query genreation , i am able to generate it but receiving output in timestamp format , please help in converting it to to_date format.
Here is my code:-
from pandas import *
table_name="ADI"
file_name=pandas.read_excel('supermarke.xlsx')
def SQL_Insert(SOURCE, TARGET):
sql_texts =
for index, row in SOURCE.iterrows():
sql_texts.append(
'INSERT INTO ' + TARGET + ' (' + str(', '.join(SOURCE.columns)) + ') VALUES ' + str(tuple(row.values))+";")
return ('n'.join(sql_texts))
print(SQL_Insert(file_name, table_name))
Here is the output i am getting :-
INSERT INTO ADI (ID, Address, City, State, Country, Supermarket Name, Number of Employees, DATE) VALUES (1, '3666 21st St', 'San Francisco', 'CA 94114', 'USA', 'Madeira', 8, Timestamp('2018-01-12 00:00:00'));
INSERT INTO ADI (ID, Address, City, State, Country, Supermarket Name, Number of Employees, DATE) VALUES (2, '735 Dolores St', 'San Francisco', 'CA 94119', 'USA', 'Bready Shop', 15, Timestamp('2018-01-12 00:00:00'));
INSERT INTO ADI (ID, Address, City, State, Country, Supermarket Name, Number of Employees, DATE) VALUES (3, '332 Hill St', 'San Francisco', 'California 94114', 'USA', 'Super River', 25, Timestamp('2018-01-12 00:00:00'));
INSERT INTO ADI (ID, Address, City, State, Country, Supermarket Name, Number of Employees, DATE) VALUES (4, '3995 23rd St', 'San Francisco', 'CA 94114', 'USA', "Ben's Shop", 10, Timestamp('2018-01-12 00:00:00'));
Here what i am expecting :-
INSERT INTO ADI (ID, Address, City, State, Country, Supermarket Name, Number of Employees, DATE) VALUES (1, '3666 21st St', 'San Francisco', 'CA 94114', 'USA', 'Madeira', 8, TO_DATE('12/01/2018 00:00:00', 'MM/DD/YYYY HH24:MI:SS');
INSERT INTO ADI (ID, Address, City, State, Country, Supermarket Name, Number of Employees, DATE) VALUES (2, '735 Dolores St', 'San Francisco', 'CA 94119', 'USA', 'Bready Shop', 15, TO_DATE('12/01/2018 00:00:00', 'MM/DD/YYYY HH24:MI:SS');
INSERT INTO ADI (ID, Address, City, State, Country, Supermarket Name, Number of Employees, DATE) VALUES (3, '332 Hill St', 'San Francisco', 'California 94114', 'USA', 'Super River', 25,TO_DATE('12/01/2018 00:00:00', 'MM/DD/YYYY HH24:MI:SS');
INSERT INTO ADI (ID, Address, City, State, Country, Supermarket Name, Number of Employees, DATE) VALUES (4, '3995 23rd St', 'San Francisco', 'CA 94114', 'USA', "Ben's Shop", 10, TO_DATE('12/01/2018 00:00:00', 'MM/DD/YYYY HH24:MI:SS');
Please help me out here
pandas
I am building and app for sql query genreation , i am able to generate it but receiving output in timestamp format , please help in converting it to to_date format.
Here is my code:-
from pandas import *
table_name="ADI"
file_name=pandas.read_excel('supermarke.xlsx')
def SQL_Insert(SOURCE, TARGET):
sql_texts =
for index, row in SOURCE.iterrows():
sql_texts.append(
'INSERT INTO ' + TARGET + ' (' + str(', '.join(SOURCE.columns)) + ') VALUES ' + str(tuple(row.values))+";")
return ('n'.join(sql_texts))
print(SQL_Insert(file_name, table_name))
Here is the output i am getting :-
INSERT INTO ADI (ID, Address, City, State, Country, Supermarket Name, Number of Employees, DATE) VALUES (1, '3666 21st St', 'San Francisco', 'CA 94114', 'USA', 'Madeira', 8, Timestamp('2018-01-12 00:00:00'));
INSERT INTO ADI (ID, Address, City, State, Country, Supermarket Name, Number of Employees, DATE) VALUES (2, '735 Dolores St', 'San Francisco', 'CA 94119', 'USA', 'Bready Shop', 15, Timestamp('2018-01-12 00:00:00'));
INSERT INTO ADI (ID, Address, City, State, Country, Supermarket Name, Number of Employees, DATE) VALUES (3, '332 Hill St', 'San Francisco', 'California 94114', 'USA', 'Super River', 25, Timestamp('2018-01-12 00:00:00'));
INSERT INTO ADI (ID, Address, City, State, Country, Supermarket Name, Number of Employees, DATE) VALUES (4, '3995 23rd St', 'San Francisco', 'CA 94114', 'USA', "Ben's Shop", 10, Timestamp('2018-01-12 00:00:00'));
Here what i am expecting :-
INSERT INTO ADI (ID, Address, City, State, Country, Supermarket Name, Number of Employees, DATE) VALUES (1, '3666 21st St', 'San Francisco', 'CA 94114', 'USA', 'Madeira', 8, TO_DATE('12/01/2018 00:00:00', 'MM/DD/YYYY HH24:MI:SS');
INSERT INTO ADI (ID, Address, City, State, Country, Supermarket Name, Number of Employees, DATE) VALUES (2, '735 Dolores St', 'San Francisco', 'CA 94119', 'USA', 'Bready Shop', 15, TO_DATE('12/01/2018 00:00:00', 'MM/DD/YYYY HH24:MI:SS');
INSERT INTO ADI (ID, Address, City, State, Country, Supermarket Name, Number of Employees, DATE) VALUES (3, '332 Hill St', 'San Francisco', 'California 94114', 'USA', 'Super River', 25,TO_DATE('12/01/2018 00:00:00', 'MM/DD/YYYY HH24:MI:SS');
INSERT INTO ADI (ID, Address, City, State, Country, Supermarket Name, Number of Employees, DATE) VALUES (4, '3995 23rd St', 'San Francisco', 'CA 94114', 'USA', "Ben's Shop", 10, TO_DATE('12/01/2018 00:00:00', 'MM/DD/YYYY HH24:MI:SS');
Please help me out here
pandas
pandas
edited Nov 26 '18 at 16:40
aditya singh
asked Nov 24 '18 at 16:32
aditya singhaditya singh
144
144
Quick fix: in your loop, first build the date string usingstrftime, then include it into the string withstr(tuple(row.values[:-1])) + custom_date_str + ";". Bigger fix: I suggest not manually building SQL query strings, and using a library like PyPika.
– Peter Leimbigler
Nov 24 '18 at 16:42
@PeterLeimbigler, please elaborate , i didn't get it .Thanks
– aditya singh
Nov 24 '18 at 16:50
add a comment |
Quick fix: in your loop, first build the date string usingstrftime, then include it into the string withstr(tuple(row.values[:-1])) + custom_date_str + ";". Bigger fix: I suggest not manually building SQL query strings, and using a library like PyPika.
– Peter Leimbigler
Nov 24 '18 at 16:42
@PeterLeimbigler, please elaborate , i didn't get it .Thanks
– aditya singh
Nov 24 '18 at 16:50
Quick fix: in your loop, first build the date string using
strftime, then include it into the string with str(tuple(row.values[:-1])) + custom_date_str + ";". Bigger fix: I suggest not manually building SQL query strings, and using a library like PyPika.– Peter Leimbigler
Nov 24 '18 at 16:42
Quick fix: in your loop, first build the date string using
strftime, then include it into the string with str(tuple(row.values[:-1])) + custom_date_str + ";". Bigger fix: I suggest not manually building SQL query strings, and using a library like PyPika.– Peter Leimbigler
Nov 24 '18 at 16:42
@PeterLeimbigler, please elaborate , i didn't get it .Thanks
– aditya singh
Nov 24 '18 at 16:50
@PeterLeimbigler, please elaborate , i didn't get it .Thanks
– aditya singh
Nov 24 '18 at 16:50
add a comment |
1 Answer
1
active
oldest
votes
If you must manually stitch together SQL query strings, try this (untested since I don't have your data):
import pandas as pd
table_name = 'ADI'
df = pandas.read_excel('supermarke.xlsx')
def SQL_Insert(SOURCE, TARGET):
sql_texts =
for index, row in SOURCE.iterrows():
formatted_date = pd.to_datetime(row[-1]).strftime('%m/%d/%Y %H:%M:%S')
date_str = 'TO_DATE('{}', 'MM/DD/YYYY HH24:MI:SS')'.format(formatted_date)
sql_texts.append((
'INSERT INTO ' +
TARGET +
' (' +
str(', '.join(SOURCE.columns)) + ') VALUES ' +
str(tuple(row.values[:-1])) +
date_str +
';'))
return ('n'.join(sql_texts))
print(SQL_Insert(df, table_name))
I recommend not manually building query strings. It's a solved problem. Further reading: How do I get a raw, compiled SQL query from a SQLAlchemy expression?
i am getting below error:- formatted_date = row[-1].datetime.strptime('%m/%d/%Y %H:%M:%S') AttributeError: 'str' object has no attribute 'datetime'
– aditya singh
Nov 24 '18 at 17:29
Try what I actually wrote, which isformatted_date = row[-1].strftime('%m/%d/%Y %H:%M:%S')(no.datetime)
– Peter Leimbigler
Nov 24 '18 at 20:34
I tried the same getting below error. formatted_date = row[-1].strftime('%m/%d/%Y %H:%M:%S') AttributeError: 'str' object has no attribute 'strftime'
– aditya singh
Nov 25 '18 at 12:10
@adityasingh, then you have to runpd.to_datetime()on your date column to convert it from string format. I've edited my answer, but without seeing your data, I can't be sure if it will work.
– Peter Leimbigler
Nov 25 '18 at 16:49
Still Receiving the error File "C:UsersadiecAppDataLocalProgramsPythonPython37-32libsite-packagesdateutilparser_parser.py", line 1356, in parse return DEFAULTPARSER.parse(timestr, **kwargs) File "C:UsersadiecAppDataLocalProgramsPythonPython37-32libsite-packagesdateutilparser_parser.py", line 648, in parse raise ValueError("Unknown string format:", timestr) ValueError: ('Unknown string format:', 'null') Editted the question added input data
– aditya singh
Nov 26 '18 at 16:40
|
show 1 more comment
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53460174%2fconverting-time-stamp-to-to-date-in-pandas%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
If you must manually stitch together SQL query strings, try this (untested since I don't have your data):
import pandas as pd
table_name = 'ADI'
df = pandas.read_excel('supermarke.xlsx')
def SQL_Insert(SOURCE, TARGET):
sql_texts =
for index, row in SOURCE.iterrows():
formatted_date = pd.to_datetime(row[-1]).strftime('%m/%d/%Y %H:%M:%S')
date_str = 'TO_DATE('{}', 'MM/DD/YYYY HH24:MI:SS')'.format(formatted_date)
sql_texts.append((
'INSERT INTO ' +
TARGET +
' (' +
str(', '.join(SOURCE.columns)) + ') VALUES ' +
str(tuple(row.values[:-1])) +
date_str +
';'))
return ('n'.join(sql_texts))
print(SQL_Insert(df, table_name))
I recommend not manually building query strings. It's a solved problem. Further reading: How do I get a raw, compiled SQL query from a SQLAlchemy expression?
i am getting below error:- formatted_date = row[-1].datetime.strptime('%m/%d/%Y %H:%M:%S') AttributeError: 'str' object has no attribute 'datetime'
– aditya singh
Nov 24 '18 at 17:29
Try what I actually wrote, which isformatted_date = row[-1].strftime('%m/%d/%Y %H:%M:%S')(no.datetime)
– Peter Leimbigler
Nov 24 '18 at 20:34
I tried the same getting below error. formatted_date = row[-1].strftime('%m/%d/%Y %H:%M:%S') AttributeError: 'str' object has no attribute 'strftime'
– aditya singh
Nov 25 '18 at 12:10
@adityasingh, then you have to runpd.to_datetime()on your date column to convert it from string format. I've edited my answer, but without seeing your data, I can't be sure if it will work.
– Peter Leimbigler
Nov 25 '18 at 16:49
Still Receiving the error File "C:UsersadiecAppDataLocalProgramsPythonPython37-32libsite-packagesdateutilparser_parser.py", line 1356, in parse return DEFAULTPARSER.parse(timestr, **kwargs) File "C:UsersadiecAppDataLocalProgramsPythonPython37-32libsite-packagesdateutilparser_parser.py", line 648, in parse raise ValueError("Unknown string format:", timestr) ValueError: ('Unknown string format:', 'null') Editted the question added input data
– aditya singh
Nov 26 '18 at 16:40
|
show 1 more comment
If you must manually stitch together SQL query strings, try this (untested since I don't have your data):
import pandas as pd
table_name = 'ADI'
df = pandas.read_excel('supermarke.xlsx')
def SQL_Insert(SOURCE, TARGET):
sql_texts =
for index, row in SOURCE.iterrows():
formatted_date = pd.to_datetime(row[-1]).strftime('%m/%d/%Y %H:%M:%S')
date_str = 'TO_DATE('{}', 'MM/DD/YYYY HH24:MI:SS')'.format(formatted_date)
sql_texts.append((
'INSERT INTO ' +
TARGET +
' (' +
str(', '.join(SOURCE.columns)) + ') VALUES ' +
str(tuple(row.values[:-1])) +
date_str +
';'))
return ('n'.join(sql_texts))
print(SQL_Insert(df, table_name))
I recommend not manually building query strings. It's a solved problem. Further reading: How do I get a raw, compiled SQL query from a SQLAlchemy expression?
i am getting below error:- formatted_date = row[-1].datetime.strptime('%m/%d/%Y %H:%M:%S') AttributeError: 'str' object has no attribute 'datetime'
– aditya singh
Nov 24 '18 at 17:29
Try what I actually wrote, which isformatted_date = row[-1].strftime('%m/%d/%Y %H:%M:%S')(no.datetime)
– Peter Leimbigler
Nov 24 '18 at 20:34
I tried the same getting below error. formatted_date = row[-1].strftime('%m/%d/%Y %H:%M:%S') AttributeError: 'str' object has no attribute 'strftime'
– aditya singh
Nov 25 '18 at 12:10
@adityasingh, then you have to runpd.to_datetime()on your date column to convert it from string format. I've edited my answer, but without seeing your data, I can't be sure if it will work.
– Peter Leimbigler
Nov 25 '18 at 16:49
Still Receiving the error File "C:UsersadiecAppDataLocalProgramsPythonPython37-32libsite-packagesdateutilparser_parser.py", line 1356, in parse return DEFAULTPARSER.parse(timestr, **kwargs) File "C:UsersadiecAppDataLocalProgramsPythonPython37-32libsite-packagesdateutilparser_parser.py", line 648, in parse raise ValueError("Unknown string format:", timestr) ValueError: ('Unknown string format:', 'null') Editted the question added input data
– aditya singh
Nov 26 '18 at 16:40
|
show 1 more comment
If you must manually stitch together SQL query strings, try this (untested since I don't have your data):
import pandas as pd
table_name = 'ADI'
df = pandas.read_excel('supermarke.xlsx')
def SQL_Insert(SOURCE, TARGET):
sql_texts =
for index, row in SOURCE.iterrows():
formatted_date = pd.to_datetime(row[-1]).strftime('%m/%d/%Y %H:%M:%S')
date_str = 'TO_DATE('{}', 'MM/DD/YYYY HH24:MI:SS')'.format(formatted_date)
sql_texts.append((
'INSERT INTO ' +
TARGET +
' (' +
str(', '.join(SOURCE.columns)) + ') VALUES ' +
str(tuple(row.values[:-1])) +
date_str +
';'))
return ('n'.join(sql_texts))
print(SQL_Insert(df, table_name))
I recommend not manually building query strings. It's a solved problem. Further reading: How do I get a raw, compiled SQL query from a SQLAlchemy expression?
If you must manually stitch together SQL query strings, try this (untested since I don't have your data):
import pandas as pd
table_name = 'ADI'
df = pandas.read_excel('supermarke.xlsx')
def SQL_Insert(SOURCE, TARGET):
sql_texts =
for index, row in SOURCE.iterrows():
formatted_date = pd.to_datetime(row[-1]).strftime('%m/%d/%Y %H:%M:%S')
date_str = 'TO_DATE('{}', 'MM/DD/YYYY HH24:MI:SS')'.format(formatted_date)
sql_texts.append((
'INSERT INTO ' +
TARGET +
' (' +
str(', '.join(SOURCE.columns)) + ') VALUES ' +
str(tuple(row.values[:-1])) +
date_str +
';'))
return ('n'.join(sql_texts))
print(SQL_Insert(df, table_name))
I recommend not manually building query strings. It's a solved problem. Further reading: How do I get a raw, compiled SQL query from a SQLAlchemy expression?
edited Nov 25 '18 at 16:49
answered Nov 24 '18 at 17:04
Peter LeimbiglerPeter Leimbigler
4,0831415
4,0831415
i am getting below error:- formatted_date = row[-1].datetime.strptime('%m/%d/%Y %H:%M:%S') AttributeError: 'str' object has no attribute 'datetime'
– aditya singh
Nov 24 '18 at 17:29
Try what I actually wrote, which isformatted_date = row[-1].strftime('%m/%d/%Y %H:%M:%S')(no.datetime)
– Peter Leimbigler
Nov 24 '18 at 20:34
I tried the same getting below error. formatted_date = row[-1].strftime('%m/%d/%Y %H:%M:%S') AttributeError: 'str' object has no attribute 'strftime'
– aditya singh
Nov 25 '18 at 12:10
@adityasingh, then you have to runpd.to_datetime()on your date column to convert it from string format. I've edited my answer, but without seeing your data, I can't be sure if it will work.
– Peter Leimbigler
Nov 25 '18 at 16:49
Still Receiving the error File "C:UsersadiecAppDataLocalProgramsPythonPython37-32libsite-packagesdateutilparser_parser.py", line 1356, in parse return DEFAULTPARSER.parse(timestr, **kwargs) File "C:UsersadiecAppDataLocalProgramsPythonPython37-32libsite-packagesdateutilparser_parser.py", line 648, in parse raise ValueError("Unknown string format:", timestr) ValueError: ('Unknown string format:', 'null') Editted the question added input data
– aditya singh
Nov 26 '18 at 16:40
|
show 1 more comment
i am getting below error:- formatted_date = row[-1].datetime.strptime('%m/%d/%Y %H:%M:%S') AttributeError: 'str' object has no attribute 'datetime'
– aditya singh
Nov 24 '18 at 17:29
Try what I actually wrote, which isformatted_date = row[-1].strftime('%m/%d/%Y %H:%M:%S')(no.datetime)
– Peter Leimbigler
Nov 24 '18 at 20:34
I tried the same getting below error. formatted_date = row[-1].strftime('%m/%d/%Y %H:%M:%S') AttributeError: 'str' object has no attribute 'strftime'
– aditya singh
Nov 25 '18 at 12:10
@adityasingh, then you have to runpd.to_datetime()on your date column to convert it from string format. I've edited my answer, but without seeing your data, I can't be sure if it will work.
– Peter Leimbigler
Nov 25 '18 at 16:49
Still Receiving the error File "C:UsersadiecAppDataLocalProgramsPythonPython37-32libsite-packagesdateutilparser_parser.py", line 1356, in parse return DEFAULTPARSER.parse(timestr, **kwargs) File "C:UsersadiecAppDataLocalProgramsPythonPython37-32libsite-packagesdateutilparser_parser.py", line 648, in parse raise ValueError("Unknown string format:", timestr) ValueError: ('Unknown string format:', 'null') Editted the question added input data
– aditya singh
Nov 26 '18 at 16:40
i am getting below error:- formatted_date = row[-1].datetime.strptime('%m/%d/%Y %H:%M:%S') AttributeError: 'str' object has no attribute 'datetime'
– aditya singh
Nov 24 '18 at 17:29
i am getting below error:- formatted_date = row[-1].datetime.strptime('%m/%d/%Y %H:%M:%S') AttributeError: 'str' object has no attribute 'datetime'
– aditya singh
Nov 24 '18 at 17:29
Try what I actually wrote, which is
formatted_date = row[-1].strftime('%m/%d/%Y %H:%M:%S') (no .datetime)– Peter Leimbigler
Nov 24 '18 at 20:34
Try what I actually wrote, which is
formatted_date = row[-1].strftime('%m/%d/%Y %H:%M:%S') (no .datetime)– Peter Leimbigler
Nov 24 '18 at 20:34
I tried the same getting below error. formatted_date = row[-1].strftime('%m/%d/%Y %H:%M:%S') AttributeError: 'str' object has no attribute 'strftime'
– aditya singh
Nov 25 '18 at 12:10
I tried the same getting below error. formatted_date = row[-1].strftime('%m/%d/%Y %H:%M:%S') AttributeError: 'str' object has no attribute 'strftime'
– aditya singh
Nov 25 '18 at 12:10
@adityasingh, then you have to run
pd.to_datetime() on your date column to convert it from string format. I've edited my answer, but without seeing your data, I can't be sure if it will work.– Peter Leimbigler
Nov 25 '18 at 16:49
@adityasingh, then you have to run
pd.to_datetime() on your date column to convert it from string format. I've edited my answer, but without seeing your data, I can't be sure if it will work.– Peter Leimbigler
Nov 25 '18 at 16:49
Still Receiving the error File "C:UsersadiecAppDataLocalProgramsPythonPython37-32libsite-packagesdateutilparser_parser.py", line 1356, in parse return DEFAULTPARSER.parse(timestr, **kwargs) File "C:UsersadiecAppDataLocalProgramsPythonPython37-32libsite-packagesdateutilparser_parser.py", line 648, in parse raise ValueError("Unknown string format:", timestr) ValueError: ('Unknown string format:', 'null') Editted the question added input data
– aditya singh
Nov 26 '18 at 16:40
Still Receiving the error File "C:UsersadiecAppDataLocalProgramsPythonPython37-32libsite-packagesdateutilparser_parser.py", line 1356, in parse return DEFAULTPARSER.parse(timestr, **kwargs) File "C:UsersadiecAppDataLocalProgramsPythonPython37-32libsite-packagesdateutilparser_parser.py", line 648, in parse raise ValueError("Unknown string format:", timestr) ValueError: ('Unknown string format:', 'null') Editted the question added input data
– aditya singh
Nov 26 '18 at 16:40
|
show 1 more comment
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53460174%2fconverting-time-stamp-to-to-date-in-pandas%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Quick fix: in your loop, first build the date string using
strftime, then include it into the string withstr(tuple(row.values[:-1])) + custom_date_str + ";". Bigger fix: I suggest not manually building SQL query strings, and using a library like PyPika.– Peter Leimbigler
Nov 24 '18 at 16:42
@PeterLeimbigler, please elaborate , i didn't get it .Thanks
– aditya singh
Nov 24 '18 at 16:50